2016-12-18 02:47:13 +00:00
//
// PCMSegmentEventSourceTests.m
// Clock Signal
//
// Created by Thomas Harte on 17/12/2016.
2018-05-13 19:19:52 +00:00
// Copyright 2016 Thomas Harte. All rights reserved.
2016-12-18 02:47:13 +00:00
//
#import <XCTest/XCTest.h>
#include "PCMSegment.hpp"
@interface PCMSegmentEventSourceTests : XCTestCase
@end
@implementation PCMSegmentEventSourceTests
2019-12-22 05:00:23 +00:00
- (Storage::Disk::PCMSegmentEventSource)segmentSource {
2018-07-02 02:49:57 +00:00
std::vector<uint8_t> data = {0xff, 0x00, 0xff, 0x00};
Storage::Disk::PCMSegment alternatingFFs(Storage::Time(1, 10), data.size()*8, data);
2016-12-18 02:47:13 +00:00
return Storage::Disk::PCMSegmentEventSource(alternatingFFs);
}
2019-12-22 05:00:23 +00:00
- (void)testCentring {
2016-12-18 02:47:13 +00:00
Storage::Disk::PCMSegmentEventSource segmentSource = self.segmentSource;
[self assertFirstTwoEventLengthsForSource:segmentSource];
}
2019-12-22 05:00:23 +00:00
- (void)assertFirstTwoEventLengthsForSource:(Storage::Disk::PCMSegmentEventSource &)segmentSource {
2016-12-18 02:47:13 +00:00
Storage::Disk::Track::Event first_event = segmentSource.get_next_event();
Storage::Disk::Track::Event second_event = segmentSource.get_next_event();
first_event.length.simplify();
second_event.length.simplify();
XCTAssertTrue(first_event.length.length == 1 && first_event.length.clock_rate == 20, @"First event should occur half a bit's length in");
XCTAssertTrue(second_event.length.length == 1 && second_event.length.clock_rate == 10, @"Second event should occur a whole bit's length after the first");
}
2019-12-22 05:00:23 +00:00
- (void)testLongerGap {
2016-12-18 02:47:13 +00:00
Storage::Disk::PCMSegmentEventSource segmentSource = self.segmentSource;
// skip first eight flux transitions
for(int c = 0; c < 8; c++) segmentSource.get_next_event();
Storage::Disk::Track::Event next_event = segmentSource.get_next_event();
next_event.length.simplify();
XCTAssertTrue(next_event.length.length == 9 && next_event.length.clock_rate == 10, @"Zero byte should give a nine bit length event gap");
}
2019-12-22 05:00:23 +00:00
- (void)testTermination {
2016-12-18 02:47:13 +00:00
Storage::Disk::PCMSegmentEventSource segmentSource = self.segmentSource;
Storage::Time total_time;
for(int c = 0; c < 16; c++) total_time += segmentSource.get_next_event().length;
Storage::Disk::Track::Event final_event = segmentSource.get_next_event();
total_time += final_event.length;
total_time.simplify();
XCTAssertTrue(final_event.type == Storage::Disk::Track::Event::IndexHole, @"Segment should end with an index hole");
XCTAssertTrue(total_time.length == 16 && total_time.clock_rate == 5, @"Should have taken 32 bit lengths to finish the segment");
}
2019-12-22 05:00:23 +00:00
- (void)testReset {
2016-12-18 02:47:13 +00:00
Storage::Disk::PCMSegmentEventSource segmentSource = self.segmentSource;
for(int c = 0; c < 8; c++) segmentSource.get_next_event();
segmentSource.reset();
[self assertFirstTwoEventLengthsForSource:segmentSource];
}
2019-12-22 05:00:23 +00:00
- (void)testSeekToSecondBit {
2016-12-18 15:19:24 +00:00
Storage::Disk::PCMSegmentEventSource segmentSource = self.segmentSource;
Storage::Time target_time(1, 10);
Storage::Time found_time = segmentSource.seek_to(target_time);
found_time.simplify();
XCTAssertTrue(found_time.length == 1 && found_time.clock_rate == 20, @"A request to seek to 1/10th should have seeked to 1/20th");
Storage::Disk::Track::Event next_event = segmentSource.get_next_event();
next_event.length.simplify();
XCTAssertTrue(next_event.length.length == 1 && next_event.length.clock_rate == 10, @"Next event should be 1/10th later");
}
2019-12-22 05:00:23 +00:00
- (void)testSeekBeyondFinalBit {
2016-12-18 15:19:24 +00:00
Storage::Disk::PCMSegmentEventSource segmentSource = self.segmentSource;
Storage::Time target_time(24, 10);
Storage::Time found_time = segmentSource.seek_to(target_time);
found_time.simplify();
XCTAssertTrue(found_time.length == 47 && found_time.clock_rate == 20, @"A request to seek to 24/10ths should have seeked to 47/20ths");
Storage::Disk::Track::Event next_event = segmentSource.get_next_event();
next_event.length.simplify();
XCTAssertTrue(next_event.length.length == 17 && next_event.length.clock_rate == 20, @"Next event should be 17/20ths later");
XCTAssertTrue(next_event.type == Storage::Disk::Track::Event::IndexHole, @"End should have been reached");
}
2016-12-18 02:47:13 +00:00
@end