From f9a5595dad438ee90b32ab11b1a72c3923c960c3 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 18 Dec 2016 10:19:24 -0500 Subject: [PATCH] Added seeking tests, correcting such errors as uncovered. --- .../PCMSegmentEventSourceTests.mm | 33 +++++++++++++++++++ Storage/Disk/PCMSegment.cpp | 8 +++-- Storage/Storage.hpp | 19 +++++++++-- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/OSBindings/Mac/Clock SignalTests/PCMSegmentEventSourceTests.mm b/OSBindings/Mac/Clock SignalTests/PCMSegmentEventSourceTests.mm index fde61678a..f40af2f28 100644 --- a/OSBindings/Mac/Clock SignalTests/PCMSegmentEventSourceTests.mm +++ b/OSBindings/Mac/Clock SignalTests/PCMSegmentEventSourceTests.mm @@ -77,4 +77,37 @@ [self assertFirstTwoEventLengthsForSource:segmentSource]; } +- (void)testSeekToSecondBit +{ + 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"); +} + +- (void)testSeekBeyondFinalBit +{ + 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"); +} + @end diff --git a/Storage/Disk/PCMSegment.cpp b/Storage/Disk/PCMSegment.cpp index 83f4dc203..fbf105363 100644 --- a/Storage/Disk/PCMSegment.cpp +++ b/Storage/Disk/PCMSegment.cpp @@ -77,10 +77,12 @@ Storage::Time PCMSegmentEventSource::seek_to(const Time &time_from_start) return zero; } - // adjust for time to get to bit zero and determine number of bits in + // adjust for time to get to bit zero and determine number of bits in; + // bit_pointer_ always records _the next bit_ that might trigger an event, + // so should be one beyond the one reached by a seek. Time relative_time = time_from_start - half_bit_length; - bit_pointer_ = (relative_time / segment_.length_of_a_bit).get_unsigned_int(); + bit_pointer_ = 1 + (relative_time / segment_.length_of_a_bit).get_unsigned_int(); // map up to the correct amount of time - return half_bit_length + segment_.length_of_a_bit * (unsigned int)bit_pointer_; + return half_bit_length + segment_.length_of_a_bit * (unsigned int)(bit_pointer_ - 1); } diff --git a/Storage/Storage.hpp b/Storage/Storage.hpp index 4e1ef640f..8077806fa 100644 --- a/Storage/Storage.hpp +++ b/Storage/Storage.hpp @@ -20,6 +20,7 @@ namespace Storage { struct Time { unsigned int length, clock_rate; Time() : length(0), clock_rate(1) {} + Time(unsigned int length, unsigned int clock_rate) : length(length), clock_rate(clock_rate) {} /*! Reduces this @c Time to its simplest form — eliminates all common factors from @c length @@ -116,8 +117,8 @@ struct Time { inline Time operator * (unsigned int multiplier) const { Time result; - result.clock_rate = clock_rate * multiplier; - result.length = length; + result.clock_rate = clock_rate; + result.length = length * multiplier; return result; } @@ -142,6 +143,20 @@ struct Time { return *this; } + inline Time operator / (unsigned int divisor) const + { + Time result; + result.length = length; + result.clock_rate = clock_rate * divisor; + return result; + } + + inline Time &operator /= (unsigned int divisor) + { + clock_rate *= divisor; + return *this; + } + inline void set_zero() { length = 0;