@@ -82,4 +82,162 @@ public void adjustSampleTimestamp_afterResetToDifferentStartTime() {
8282 assertThat (firstAdjustedTimestampUs ).isEqualTo (5000 );
8383 assertThat (secondAdjustedTimestampUs ).isEqualTo (9000 );
8484 }
85+
86+ @ Test
87+ public void
88+ adjustTsTimestamp_closeToWraparoundFollowedBySlightlySmallerValue_doesNotAssumeWraparound () {
89+ // Init timestamp with a non-zero wraparound (multiple of 33-bit) and close to the next one.
90+ TimestampAdjuster adjuster =
91+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L - 90_000 ));
92+
93+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 90_000 );
94+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 180_000 );
95+
96+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs - 1_000_000 );
97+ }
98+
99+ @ Test
100+ public void
101+ adjustTsTimestamp_closeToWraparoundFollowedBySlightlyLargerValue_doesNotAssumeWraparound () {
102+ // Init timestamp with a non-zero wraparound (multiple of 33-bit) and close to the next one.
103+ TimestampAdjuster adjuster =
104+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L - 90_000 ));
105+
106+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 90_000 );
107+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 45_000 );
108+
109+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs + 500_000 );
110+ }
111+
112+ @ Test
113+ public void adjustTsTimestamp_closeToWraparoundFollowedByMuchSmallerValue_assumesWraparound () {
114+ // Init timestamp with a non-zero wraparound (multiple of 33-bit) and close to the next one.
115+ TimestampAdjuster adjuster =
116+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L - 90_000 ));
117+
118+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 90_000 );
119+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestamp (90_000 );
120+
121+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs + 2_000_000 );
122+ }
123+
124+ @ Test
125+ public void
126+ adjustTsTimestamp_justBeyondWraparoundFollowedBySlightlySmallerValue_doesNotAssumeWraparound () {
127+ // Init timestamp with a non-zero wraparound (multiple of 33-bit), just beyond the last one.
128+ TimestampAdjuster adjuster =
129+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L + 90_000 ));
130+
131+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (90_000 );
132+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestamp (45_000 );
133+
134+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs - 500_000 );
135+ }
136+
137+ @ Test
138+ public void
139+ adjustTsTimestamp_justBeyondWraparoundFollowedBySlightlyLargerValue_doesNotAssumeWraparound () {
140+ // Init timestamp with a non-zero wraparound (multiple of 33-bit), just beyond the last one.
141+ TimestampAdjuster adjuster =
142+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L + 90_000 ));
143+
144+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (90_000 );
145+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestamp (180_000 );
146+
147+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs + 1_000_000 );
148+ }
149+
150+ @ Test
151+ public void adjustTsTimestamp_justBeyondWraparoundFollowedByMuchLargerValue_assumesWraparound () {
152+ // Init timestamp with a non-zero wraparound (multiple of 33-bit), just beyond the last one.
153+ TimestampAdjuster adjuster =
154+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L + 90_000 ));
155+
156+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (90_000 );
157+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 90_000 );
158+
159+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs - 2_000_000 );
160+ }
161+
162+ @ Test
163+ public void
164+ adjustTsTimestampGreaterThanPreviousTimestamp_closeToWraparoundFollowedBySlightlySmallerValue_assumesWraparound () {
165+ // Init timestamp with a non-zero wraparound (multiple of 33-bit) and close to the next one.
166+ TimestampAdjuster adjuster =
167+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L - 90_000 ));
168+
169+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 90_000 );
170+ long secondAdjustedTimestampUs =
171+ adjuster .adjustTsTimestampGreaterThanPreviousTimestamp (0x200000000L - 180_000 );
172+
173+ assertThat (secondAdjustedTimestampUs - firstAdjustedTimestampUs ).isGreaterThan (0x100000000L );
174+ }
175+
176+ @ Test
177+ public void
178+ adjustTsTimestampGreaterThanPreviousTimestamp_closeToWraparoundFollowedBySlightlyLargerValue_doesNotAssumeWraparound () {
179+ // Init timestamp with a non-zero wraparound (multiple of 33-bit) and close to the next one.
180+ TimestampAdjuster adjuster =
181+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L - 90_000 ));
182+
183+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 90_000 );
184+ long secondAdjustedTimestampUs =
185+ adjuster .adjustTsTimestampGreaterThanPreviousTimestamp (0x200000000L - 45_000 );
186+
187+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs + 500_000 );
188+ }
189+
190+ @ Test
191+ public void
192+ adjustTsTimestampGreaterThanPreviousTimestamp_closeToWraparoundFollowedByMuchSmallerValue_assumesWraparound () {
193+ // Init timestamp with a non-zero wraparound (multiple of 33-bit) and close to the next one.
194+ TimestampAdjuster adjuster =
195+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L - 90_000 ));
196+
197+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (0x200000000L - 90_000 );
198+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestampGreaterThanPreviousTimestamp (90_000 );
199+
200+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs + 2_000_000 );
201+ }
202+
203+ @ Test
204+ public void
205+ adjustTsTimestampGreaterThanPreviousTimestamp_justBeyondWraparoundFollowedBySlightlySmallerValue_assumesWraparound () {
206+ // Init timestamp with a non-zero wraparound (multiple of 33-bit), just beyond the last one.
207+ TimestampAdjuster adjuster =
208+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L + 90_000 ));
209+
210+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (90_000 );
211+ long secondAdjustedTimestampUs = adjuster .adjustTsTimestampGreaterThanPreviousTimestamp (45_000 );
212+
213+ assertThat (secondAdjustedTimestampUs - firstAdjustedTimestampUs ).isGreaterThan (0x100000000L );
214+ }
215+
216+ @ Test
217+ public void
218+ adjustTsTimestampGreaterThanPreviousTimestamp_justBeyondWraparoundFollowedBySlightlyLargerValue_doesNotAssumeWraparound () {
219+ // Init timestamp with a non-zero wraparound (multiple of 33-bit), just beyond the last one.
220+ TimestampAdjuster adjuster =
221+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L + 90_000 ));
222+
223+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (90_000 );
224+ long secondAdjustedTimestampUs =
225+ adjuster .adjustTsTimestampGreaterThanPreviousTimestamp (180_000 );
226+
227+ assertThat (secondAdjustedTimestampUs ).isEqualTo (firstAdjustedTimestampUs + 1_000_000 );
228+ }
229+
230+ @ Test
231+ public void
232+ adjustTsTimestampGreaterThanPreviousTimestamp_justBeyondWraparoundFollowedByMuchLargerValue_doesNotAssumeWraparound () {
233+ // Init timestamp with a non-zero wraparound (multiple of 33-bit), just beyond the last one.
234+ TimestampAdjuster adjuster =
235+ new TimestampAdjuster (TimestampAdjuster .ptsToUs (3 * 0x200000000L + 90_000 ));
236+
237+ long firstAdjustedTimestampUs = adjuster .adjustTsTimestamp (90_000 );
238+ long secondAdjustedTimestampUs =
239+ adjuster .adjustTsTimestampGreaterThanPreviousTimestamp (0x200000000L - 90_000 );
240+
241+ assertThat (secondAdjustedTimestampUs - firstAdjustedTimestampUs ).isGreaterThan (0x100000000L );
242+ }
85243}
0 commit comments