2016-12-17 22:05:49 +00:00
//
// PCMPatchedTrackTests.m
// Clock Signal
//
// Created by Thomas Harte on 17/12/2016.
// Copyright © 2016 Thomas Harte. All rights reserved.
//
2016-12-18 02:13:57 +00:00
#import <XCTest/XCTest.h>
2016-12-17 22:05:49 +00:00
#include "PCMTrack.hpp"
#include "PCMPatchedTrack.hpp"
2016-12-18 02:13:57 +00:00
@interface PCMPatchedTrackTests : XCTestCase
@end
2016-12-17 22:05:49 +00:00
@implementation PCMPatchedTrackTests
2016-12-22 00:54:19 +00:00
#pragma mark - Prebuilt tracks
2016-12-17 23:17:22 +00:00
- (std::shared_ptr<Storage::Disk::Track>)togglingTrack
2016-12-17 22:05:49 +00:00
{
Storage::Disk::PCMSegment segment;
segment.data = { 0xff, 0xff, 0xff, 0xff };
segment.number_of_bits = 32;
2016-12-17 23:17:22 +00:00
return std::shared_ptr<Storage::Disk::Track>(new Storage::Disk::PCMTrack(segment));
2016-12-17 22:05:49 +00:00
}
2016-12-17 23:17:22 +00:00
- (std::shared_ptr<Storage::Disk::Track>)patchableTogglingTrack
2016-12-17 22:05:49 +00:00
{
2016-12-17 23:17:22 +00:00
std::shared_ptr<Storage::Disk::Track> track = self.togglingTrack;
return std::shared_ptr<Storage::Disk::Track>(new Storage::Disk::PCMPatchedTrack(track));
}
2016-12-17 22:05:49 +00:00
2016-12-22 00:54:19 +00:00
- (std::shared_ptr<Storage::Disk::Track>)fourSegmentPatchedTrack
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.patchableTogglingTrack;
Storage::Disk::PCMPatchedTrack *patchable = static_cast<Storage::Disk::PCMPatchedTrack *>(patchableTrack.get());
for(int c = 0; c < 4; c++)
{
Storage::Disk::PCMSegment segment;
segment.data = {0xff};
segment.number_of_bits = 8;
segment.length_of_a_bit.length = 1;
segment.length_of_a_bit.clock_rate = 32;
patchable->add_segment(Storage::Time(c, 4), segment);
}
return patchableTrack;
}
#pragma mark -
- (std::vector<Storage::Disk::Track::Event>)eventsFromTrack:(std::shared_ptr<Storage::Disk::Track>)track
{
std::vector<Storage::Disk::Track::Event> events;
while(1)
{
events.push_back(track->get_next_event());
if(events.back().type == Storage::Disk::Track::Event::IndexHole) break;
}
return events;
}
- (Storage::Time)timeForEvents:(const std::vector<Storage::Disk::Track::Event> &)events
{
Storage::Time result(0);
for(auto event : events)
{
result += event.length;
}
return result;
}
2016-12-22 03:17:00 +00:00
- (void)patchTrack:(std::shared_ptr<Storage::Disk::Track>)track withSegment:(Storage::Disk::PCMSegment)segment atTime:(Storage::Time)time
{
Storage::Disk::PCMPatchedTrack *patchable = static_cast<Storage::Disk::PCMPatchedTrack *>(track.get());
patchable->add_segment(time, segment);
}
2016-12-22 00:54:19 +00:00
#pragma mark - Repeating Asserts
2016-12-17 23:17:22 +00:00
- (void)assertOneThirtyTwosForTrack:(std::shared_ptr<Storage::Disk::Track>)track
{
2016-12-17 22:05:49 +00:00
// Confirm that there are now flux transitions (just the first five will do)
// located 1/32nd of a rotation apart.
2016-12-21 00:15:36 +00:00
for(int c = 0; c < 5; c++)
2016-12-17 22:05:49 +00:00
{
2016-12-17 23:17:22 +00:00
Storage::Disk::Track::Event event = track->get_next_event();
2016-12-21 00:15:36 +00:00
XCTAssert(
event.length == (c ? Storage::Time(1, 32) : Storage::Time(1, 64)),
@"flux transitions should be 1/32nd of a track apart");
2016-12-17 22:05:49 +00:00
}
}
2016-12-22 23:13:10 +00:00
- (void)assertEvents:(const std::vector<Storage::Disk::Track::Event> &)events hasEntries:(size_t)numberOfEntries withEntry:(size_t)entry ofLength:(Storage::Time)time
{
XCTAssert(events.size() == numberOfEntries, @"Should be %zu total events", numberOfEntries);
XCTAssert(events[entry].length == time, @"Event %zu should have been %d/%d long, was %d/%d", entry, time.length, time.clock_rate, events[entry].length.length, events[entry].length.clock_rate);
Storage::Time eventTime = [self timeForEvents:events];
XCTAssert(eventTime == Storage::Time(1), @"Total track length should be 1; was %d/%d", eventTime.length, eventTime.clock_rate);
}
2016-12-22 00:54:19 +00:00
#pragma mark - Unpatched tracks
2016-12-17 23:17:22 +00:00
- (void)testUnpatchedRawTrack
{
[self assertOneThirtyTwosForTrack:self.togglingTrack];
}
- (void)testUnpatchedTrack
{
[self assertOneThirtyTwosForTrack:self.patchableTogglingTrack];
}
2016-12-22 00:54:19 +00:00
#pragma mark - Insertions affecting one existing segment
2016-12-21 00:15:36 +00:00
- (void)testSingleSplice
2016-12-18 02:13:57 +00:00
{
2016-12-20 12:52:14 +00:00
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.patchableTogglingTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(1, 32), 1, {0xff}) atTime:Storage::Time(3, 128)];
2016-12-20 12:52:14 +00:00
2016-12-22 00:54:19 +00:00
std::vector<Storage::Disk::Track::Event> events = [self eventsFromTrack:patchableTrack];
Storage::Time total_length = [self timeForEvents:events];
2016-12-21 00:15:36 +00:00
XCTAssert(events.size() == 33, @"Should still be 33 total events");
XCTAssert(events[0].length == Storage::Time(1, 64), @"First event should be after 1/64 as usual");
XCTAssert(events[1].length == Storage::Time(3, 128), @"Second event should be 3/128 later"); // ... as it was inserted at 3/128 and runs at the same rate as the main data, so first inserted event is at 3/128+1/64-1/64
XCTAssert(events[2].length == Storage::Time(5, 128), @"Should still be 33 total events"); // 1/64 = 2/128 to exit the patch, plus 3/128 to get to the next event, having spliced in 1/128 ahead of the normal clock
XCTAssert(total_length == Storage::Time(1), @"Total track length should still be 1");
2016-12-18 02:13:57 +00:00
}
2016-12-21 00:25:58 +00:00
- (void)testLeftReplace
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.patchableTogglingTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(1, 16), 8, {0x00}) atTime:Storage::Time(0)];
2016-12-21 00:25:58 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:17 withEntry:0 ofLength:Storage::Time(33, 64)];
2016-12-21 00:25:58 +00:00
}
- (void)testRightReplace
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.patchableTogglingTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(1, 16), 8, {0x00}) atTime:Storage::Time(1, 2)];
2016-12-21 00:25:58 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:17 withEntry:16 ofLength:Storage::Time(33, 64)];
2016-12-21 00:25:58 +00:00
}
2016-12-22 00:54:19 +00:00
#pragma mark - Insertions affecting three existing segments
2016-12-21 02:38:32 +00:00
- (void)testMultiSegmentTrack
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.fourSegmentPatchedTrack;
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:33 withEntry:4 ofLength:Storage::Time(1, 32)];
2016-12-21 00:25:58 +00:00
}
2016-12-21 02:46:34 +00:00
- (void)testMultiTrimBothSideReplace
2016-12-21 02:38:32 +00:00
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.fourSegmentPatchedTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(1, 16), 8, {0x00}) atTime:Storage::Time(1, 8)];
2016-12-21 02:38:32 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:17 withEntry:4 ofLength:Storage::Time(17, 32)];
2016-12-21 02:38:32 +00:00
}
2016-12-21 02:46:34 +00:00
- (void)testMultiTrimRightReplace
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.fourSegmentPatchedTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(3, 8), 1, {0x00}) atTime:Storage::Time(1, 8)];
2016-12-21 02:46:34 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:21 withEntry:4 ofLength:Storage::Time(13, 32)];
2016-12-21 02:46:34 +00:00
}
- (void)testMultiTrimLeftReplace
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.fourSegmentPatchedTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(3, 8), 1, {0x00}) atTime:Storage::Time(1, 4)];
2016-12-21 02:46:34 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:21 withEntry:8 ofLength:Storage::Time(13, 32)];
2016-12-21 02:46:34 +00:00
}
2016-12-22 00:54:19 +00:00
#pragma mark - Insertions affecting two existing segments
- (void)testTwoSegmentOverlap
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.fourSegmentPatchedTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(1, 32), 8, {0x00}) atTime:Storage::Time(1, 8)];
2016-12-22 00:54:19 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:25 withEntry:4 ofLength:Storage::Time(9, 32)];
2016-12-22 00:54:19 +00:00
}
- (void)testTwoSegmentRightReplace
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.fourSegmentPatchedTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(3, 8), 1, {0x00}) atTime:Storage::Time(1, 8)];
2016-12-22 00:54:19 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:21 withEntry:4 ofLength:Storage::Time(13, 32)];
2016-12-22 00:54:19 +00:00
}
- (void)testTwoSegmentLeftReplace
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.fourSegmentPatchedTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(3, 8), 1, {0x00}) atTime:Storage::Time(0)];
2016-12-22 00:54:19 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:21 withEntry:0 ofLength:Storage::Time(25, 64)];
2016-12-22 00:54:19 +00:00
}
#pragma mark - Wrapping segment
- (void)testWrappingSegment
{
std::shared_ptr<Storage::Disk::Track> patchableTrack = self.patchableTogglingTrack;
2016-12-22 03:17:00 +00:00
[self patchTrack:patchableTrack withSegment:Storage::Disk::PCMSegment(Storage::Time(5, 2), 1, {0x00}) atTime:Storage::Time(0)];
2016-12-22 00:54:19 +00:00
2016-12-22 23:13:10 +00:00
[self assertEvents:[self eventsFromTrack:patchableTrack] hasEntries:1 withEntry:0 ofLength:Storage::Time(1, 1)];
2016-12-22 00:54:19 +00:00
}
2016-12-17 22:05:49 +00:00
@end