-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecode_test.go
More file actions
933 lines (903 loc) · 34.4 KB
/
Copy pathtimecode_test.go
File metadata and controls
933 lines (903 loc) · 34.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
package timecode
import (
"strings"
"testing"
"time"
)
// mustRate builds a rate or fails the test.
func mustRate(t *testing.T, name string) Rate {
t.Helper()
r, err := ParseRate(name)
if err != nil {
t.Fatalf("ParseRate(%q): %v", name, err)
}
return r
}
// mustParse parses a timecode or fails the test.
func mustParse(t *testing.T, s, rate string) Timecode {
t.Helper()
tc, err := Parse(s, mustRate(t, rate))
if err != nil {
t.Fatalf("Parse(%q, %s): %v", s, rate, err)
}
return tc
}
func TestParseRate(t *testing.T) {
cases := []struct {
name string
in string
want string
wantErr bool
}{
{"23.976", "23.976", "23.976", false},
{"23.98 alias", "23.98", "23.976", false},
{"23.976nd alias", "23.976nd", "23.976", false},
{"24", "24", "24", false},
{"24.0 alias", "24.0", "24", false},
{"25", "25", "25", false},
{"29.97", "29.97", "29.97", false},
{"29.97nd alias", "29.97nd", "29.97", false},
{"29.97df", "29.97df", "29.97df", false},
{"29.97DF upper", "29.97DF", "29.97df", false},
{"29.97Df mixed", "29.97Df", "29.97df", false},
{"30", "30", "30", false},
{"50", "50", "50", false},
{"59.94", "59.94", "59.94", false},
{"59.94df", "59.94df", "59.94df", false},
{"59.94DF upper", "59.94DF", "59.94df", false},
{"60", "60", "60", false},
{"padded", " 25 ", "25", false},
{"empty", "", "", true},
{"unknown decimal", "24.5", "", true},
{"unknown 23.976df", "23.976df", "", true},
{"unknown 30df", "30df", "", true},
{"unknown 25df", "25df", "", true},
{"bare df", "df", "", true},
{"nonsense", "banana", "", true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := ParseRate(c.in)
if c.wantErr {
if err == nil {
t.Fatalf("ParseRate(%q) = %v, want an error", c.in, got)
}
return
}
if err != nil {
t.Fatalf("ParseRate(%q): %v", c.in, err)
}
if got.String() != c.want {
t.Fatalf("ParseRate(%q) = %s, want %s", c.in, got, c.want)
}
})
}
}
func TestRateProperties(t *testing.T) {
cases := []struct {
rate string
nominal int
num, den int
drop int
separator byte
framesPerHr int
framesPerDay int
}{
{"23.976", 24, 24000, 1001, 0, ':', 86400, 2073600},
{"24", 24, 24, 1, 0, ':', 86400, 2073600},
{"25", 25, 25, 1, 0, ':', 90000, 2160000},
{"29.97", 30, 30000, 1001, 0, ':', 108000, 2592000},
{"29.97df", 30, 30000, 1001, 2, ';', 107892, 2589408},
{"30", 30, 30, 1, 0, ':', 108000, 2592000},
{"50", 50, 50, 1, 0, ':', 180000, 4320000},
{"59.94", 60, 60000, 1001, 0, ':', 216000, 5184000},
{"59.94df", 60, 60000, 1001, 4, ';', 215784, 5178816},
{"60", 60, 60, 1, 0, ':', 216000, 5184000},
}
for _, c := range cases {
t.Run(c.rate, func(t *testing.T) {
r := mustRate(t, c.rate)
if r.Nominal() != c.nominal {
t.Errorf("Nominal = %d, want %d", r.Nominal(), c.nominal)
}
if r.Numerator() != c.num || r.Denominator() != c.den {
t.Errorf("rate = %d/%d, want %d/%d", r.Numerator(), r.Denominator(), c.num, c.den)
}
if r.DropCount() != c.drop {
t.Errorf("DropCount = %d, want %d", r.DropCount(), c.drop)
}
if r.Drop() != (c.drop > 0) {
t.Errorf("Drop = %v, want %v", r.Drop(), c.drop > 0)
}
if r.Separator() != c.separator {
t.Errorf("Separator = %q, want %q", r.Separator(), c.separator)
}
if r.FramesPerHour() != c.framesPerHr {
t.Errorf("FramesPerHour = %d, want %d", r.FramesPerHour(), c.framesPerHr)
}
if r.FramesPerDay() != c.framesPerDay {
t.Errorf("FramesPerDay = %d, want %d", r.FramesPerDay(), c.framesPerDay)
}
if r.Exact() != (c.den == 1) {
t.Errorf("Exact = %v, want %v", r.Exact(), c.den == 1)
}
})
}
}
// TestParseFrames covers the frame count of a timecode at every rate. The drop
// frame rows are the ones that catch a wrong implementation.
func TestParseFrames(t *testing.T) {
cases := []struct {
name string
in string
rate string
want int
print string // expected String output, "" means same as in
}{
// 29.97df, the classic values.
{"df zero", "00:00:00;00", "29.97df", 0, ""},
{"df first second", "00:00:00;29", "29.97df", 29, ""},
{"df last frame before first drop", "00:00:59;29", "29.97df", 1799, ""},
{"df 00:01:00;02 is frame 1800", "00:01:00;02", "29.97df", 1800, ""},
{"df 00:01:00;03", "00:01:00;03", "29.97df", 1801, ""},
{"df end of minute one", "00:01:59;29", "29.97df", 3597, ""},
{"df start of minute two", "00:02:00;02", "29.97df", 3598, ""},
{"df end of minute nine", "00:09:59;29", "29.97df", 17981, ""},
{"df 00:10:00;00 is frame 17982", "00:10:00;00", "29.97df", 17982, ""},
{"df tenth minute keeps 01", "00:10:00;01", "29.97df", 17983, ""},
{"df tenth minute keeps 02", "00:10:00;02", "29.97df", 17984, ""},
{"df end of minute ten", "00:10:59;29", "29.97df", 19781, ""},
{"df start of minute eleven", "00:11:00;02", "29.97df", 19782, ""},
{"df one frame before the hour", "00:59:59;29", "29.97df", 107891, ""},
{"df one hour is 107892", "01:00:00;00", "29.97df", 107892, ""},
{"df last frame of the day", "23:59:59;29", "29.97df", 2589407, ""},
{"df colon input becomes semicolon", "01:00:00:00", "29.97df", 107892, "01:00:00;00"},
{"df all semicolons", "01;00;00;00", "29.97df", 107892, "01:00:00;00"},
{"df padded input", " 01:00:00;00 ", "29.97df", 107892, "01:00:00;00"},
// 59.94df drops four labels instead of two.
{"df59 zero", "00:00:00;00", "59.94df", 0, ""},
{"df59 last frame before first drop", "00:00:59;59", "59.94df", 3599, ""},
{"df59 00:01:00;04 is frame 3600", "00:01:00;04", "59.94df", 3600, ""},
{"df59 00:01:00;05", "00:01:00;05", "59.94df", 3601, ""},
{"df59 tenth minute", "00:10:00;00", "59.94df", 35964, ""},
{"df59 tenth minute keeps 03", "00:10:00;03", "59.94df", 35967, ""},
{"df59 one hour is 215784", "01:00:00;00", "59.94df", 215784, ""},
{"df59 last frame of the day", "23:59:59;59", "59.94df", 5178815, ""},
// Non drop rates.
{"23.976 hour", "01:00:00:00", "23.976", 86400, ""},
{"23.976 last frame of day", "23:59:59:23", "23.976", 2073599, ""},
{"24 hour", "01:00:00:00", "24", 86400, ""},
{"24 one second", "00:00:01:00", "24", 24, ""},
{"25 hour", "01:00:00:00", "25", 90000, ""},
{"25 ten seconds", "00:00:10:00", "25", 250, ""},
{"25 last frame of day", "23:59:59:24", "25", 2159999, ""},
{"29.97 non drop hour", "01:00:00:00", "29.97", 108000, ""},
{"29.97 non drop minute", "00:01:00:00", "29.97", 1800, ""},
{"29.97 non drop keeps frame 00", "00:01:00:00", "29.97", 1800, ""},
{"29.97 semicolon input becomes colon", "01:00:00;00", "29.97", 108000, "01:00:00:00"},
{"30 hour", "01:00:00:00", "30", 108000, ""},
{"30 last frame of day", "23:59:59:29", "30", 2591999, ""},
{"50 hour", "01:00:00:00", "50", 180000, ""},
{"59.94 hour", "01:00:00:00", "59.94", 216000, ""},
{"60 hour", "01:00:00:00", "60", 216000, ""},
{"60 last frame of day", "23:59:59:59", "60", 5183999, ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
tc, err := Parse(c.in, mustRate(t, c.rate))
if err != nil {
t.Fatalf("Parse(%q, %s): %v", c.in, c.rate, err)
}
if tc.Frames() != c.want {
t.Fatalf("Parse(%q, %s).Frames() = %d, want %d", c.in, c.rate, tc.Frames(), c.want)
}
want := c.print
if want == "" {
want = strings.TrimSpace(c.in)
}
if got := tc.String(); got != want {
t.Fatalf("Parse(%q, %s).String() = %q, want %q", c.in, c.rate, got, want)
}
back := FromFrames(c.want, mustRate(t, c.rate))
if back.String() != want {
t.Fatalf("FromFrames(%d, %s) = %q, want %q", c.want, c.rate, back, want)
}
})
}
}
// TestFullHourRoundTrip checks one hour of timecode at every supported rate.
func TestFullHourRoundTrip(t *testing.T) {
cases := []struct {
rate string
text string
frames int
}{
{"23.976", "01:00:00:00", 86400},
{"24", "01:00:00:00", 86400},
{"25", "01:00:00:00", 90000},
{"29.97", "01:00:00:00", 108000},
{"29.97df", "01:00:00;00", 107892},
{"30", "01:00:00:00", 108000},
{"50", "01:00:00:00", 180000},
{"59.94", "01:00:00:00", 216000},
{"59.94df", "01:00:00;00", 215784},
{"60", "01:00:00:00", 216000},
}
for _, c := range cases {
t.Run(c.rate, func(t *testing.T) {
r := mustRate(t, c.rate)
tc := mustParse(t, c.text, c.rate)
if tc.Frames() != c.frames {
t.Fatalf("one hour at %s = %d frames, want %d", c.rate, tc.Frames(), c.frames)
}
if r.FramesPerHour() != c.frames {
t.Fatalf("FramesPerHour at %s = %d, want %d", c.rate, r.FramesPerHour(), c.frames)
}
if got := FromFrames(c.frames, r).String(); got != c.text {
t.Fatalf("FromFrames(%d, %s) = %q, want %q", c.frames, c.rate, got, c.text)
}
if r.FramesPerDay() != c.frames*24 {
t.Fatalf("FramesPerDay at %s = %d, want %d", c.rate, r.FramesPerDay(), c.frames*24)
}
})
}
}
// TestString covers formatting straight from a frame count, including the
// negative and past midnight forms.
func TestString(t *testing.T) {
cases := []struct {
name string
frames int
rate string
want string
}{
{"zero at 25", 0, "25", "00:00:00:00"},
{"hour at 25", 90000, "25", "01:00:00:00"},
{"one frame back at 25", -1, "25", "-00:00:00:01"},
{"one second back at 25", -25, "25", "-00:00:01:00"},
{"hour back at 24", -86400, "24", "-01:00:00:00"},
{"df frame 1800", 1800, "29.97df", "00:01:00;02"},
{"df frame 17982", 17982, "29.97df", "00:10:00;00"},
{"df frame 107892", 107892, "29.97df", "01:00:00;00"},
{"df last frame of day", 2589407, "29.97df", "23:59:59;29"},
{"df one day exactly", 2589408, "29.97df", "24:00:00;00"},
{"df negative minute", -1800, "29.97df", "-00:01:00;02"},
{"non drop 107892 is not an hour", 107892, "29.97", "00:59:56:12"},
{"non drop hour", 108000, "29.97", "01:00:00:00"},
{"df59 frame 3600", 3600, "59.94df", "00:01:00;04"},
{"df59 hour", 215784, "59.94df", "01:00:00;00"},
{"df59 last frame of day", 5178815, "59.94df", "23:59:59;59"},
{"23.976 hour", 86400, "23.976", "01:00:00:00"},
{"50 hour", 180000, "50", "01:00:00:00"},
{"60 half hour", 108000, "60", "00:30:00:00"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := FromFrames(c.frames, mustRate(t, c.rate)).String()
if got != c.want {
t.Fatalf("FromFrames(%d, %s) = %q, want %q", c.frames, c.rate, got, c.want)
}
})
}
}
// TestParseErrors covers every way a timecode string can be refused.
func TestParseErrors(t *testing.T) {
cases := []struct {
name string
in string
rate string
want string
}{
{"empty", "", "25", "empty"},
{"only a minus", "-", "25", "empty"},
{"three fields", "01:00:00", "25", "expected four fields"},
{"two fields", "01:00", "25", "expected four fields"},
{"one field", "01", "25", "expected four fields"},
{"five fields", "01:00:00:00:00", "25", "too many separators"},
{"decimal point", "01:00:00.00", "25", "expected four fields"},
{"comma separator", "01,00,00,00", "25", "expected four fields"},
{"one digit hours", "1:00:00:00", "25", "hours field"},
{"one digit minutes", "01:0:00:00", "25", "minutes field"},
{"one digit seconds", "01:00:0:00", "25", "seconds field"},
{"one digit frames", "01:00:00:0", "25", "frames field"},
{"three digit hours", "001:00:00:00", "25", "hours field"},
{"three digit frames", "01:00:00:000", "25", "frames field"},
{"letters in hours", "aa:00:00:00", "25", "hours field"},
{"letters in frames", "01:00:00:0a", "25", "frames field"},
{"trailing text", "01:00:00:00x", "25", "frames field"},
{"hours 24", "24:00:00:00", "25", "hours must be 00 to 23"},
{"hours 99", "99:00:00:00", "25", "hours must be 00 to 23"},
{"negative hours 24", "-24:00:00:00", "25", "hours must be 00 to 23"},
{"minutes 60", "01:60:00:00", "25", "minutes must be 00 to 59"},
{"minutes 99", "01:99:00:00", "25", "minutes must be 00 to 59"},
{"seconds 60", "01:00:60:00", "25", "seconds must be 00 to 59"},
{"frames 25 at 25", "01:00:00:25", "25", "frames must be 00 to 24"},
{"frames 24 at 24", "01:00:00:24", "24", "frames must be 00 to 23"},
{"frames 24 at 23.976", "01:00:00:24", "23.976", "frames must be 00 to 23"},
{"frames 30 at 30", "01:00:00:30", "30", "frames must be 00 to 29"},
{"frames 30 at 29.97", "01:00:00:30", "29.97", "frames must be 00 to 29"},
{"frames 30 at 29.97df", "01:00:00:30", "29.97df", "frames must be 00 to 29"},
{"frames 50 at 50", "01:00:00:50", "50", "frames must be 00 to 49"},
{"frames 60 at 60", "01:00:00:60", "60", "frames must be 00 to 59"},
{"frames 60 at 59.94df", "01:00:00:60", "59.94df", "frames must be 00 to 59"},
{"df dropped label 00 minute 1", "00:01:00;00", "29.97df", "do not exist"},
{"df dropped label 01 minute 1", "00:01:00;01", "29.97df", "do not exist"},
{"df dropped label with colon", "00:01:00:01", "29.97df", "do not exist"},
{"df dropped label minute 9", "00:09:00;01", "29.97df", "do not exist"},
{"df dropped label minute 11", "00:11:00;01", "29.97df", "do not exist"},
{"df dropped label minute 61", "01:01:00;01", "29.97df", "do not exist"},
{"df dropped label minute 1439", "23:59:00;00", "29.97df", "do not exist"},
{"df59 dropped label 00", "00:01:00;00", "59.94df", "do not exist"},
{"df59 dropped label 01", "00:01:00;01", "59.94df", "do not exist"},
{"df59 dropped label 02", "00:01:00;02", "59.94df", "do not exist"},
{"df59 dropped label 03", "00:01:00;03", "59.94df", "do not exist"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
tc, err := Parse(c.in, mustRate(t, c.rate))
if err == nil {
t.Fatalf("Parse(%q, %s) = %v, want an error", c.in, c.rate, tc)
}
if !strings.Contains(err.Error(), c.want) {
t.Fatalf("Parse(%q, %s) error = %q, want it to mention %q", c.in, c.rate, err, c.want)
}
if !strings.Contains(err.Error(), "bad timecode") {
t.Fatalf("Parse(%q, %s) error = %q, want it to start with a clear label", c.in, c.rate, err)
}
})
}
}
// TestDropFrameEdgesAccepted covers labels that look like they should be
// dropped but are not. Drop frame only skips labels at second 00, and only in
// minutes that are not divisible by ten.
func TestDropFrameEdgesAccepted(t *testing.T) {
cases := []struct {
name string
in string
rate string
frames int
}{
{"minute 0 keeps 00", "00:00:00;00", "29.97df", 0},
{"minute 0 keeps 01", "00:00:00;01", "29.97df", 1},
{"minute 10 keeps 00", "00:10:00;00", "29.97df", 17982},
{"minute 10 keeps 01", "00:10:00;01", "29.97df", 17983},
{"minute 20 keeps 00", "00:20:00;00", "29.97df", 35964},
{"minute 60 keeps 00", "01:00:00;00", "29.97df", 107892},
{"second 01 keeps 00", "00:01:01;00", "29.97df", 1828},
{"second 01 keeps 01", "00:01:01;01", "29.97df", 1829},
{"hour 1 second 1 frame 1", "01:00:01;01", "29.97df", 107923},
{"minute 1 frame 02", "00:01:00;02", "29.97df", 1800},
{"df59 minute 10 keeps 00", "00:10:00;00", "59.94df", 35964},
{"df59 minute 1 frame 04", "00:01:00;04", "59.94df", 3600},
{"df59 second 01 keeps 00", "00:01:01;00", "59.94df", 3656},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
tc, err := Parse(c.in, mustRate(t, c.rate))
if err != nil {
t.Fatalf("Parse(%q, %s): %v", c.in, c.rate, err)
}
if tc.Frames() != c.frames {
t.Fatalf("Parse(%q, %s).Frames() = %d, want %d", c.in, c.rate, tc.Frames(), c.frames)
}
})
}
}
func TestAdd(t *testing.T) {
cases := []struct {
name string
a, b string
rate string
want string
frames int
days int
}{
{"df hour plus two and a half minutes", "01:00:00:00", "00:02:30:15", "29.97df", "01:02:30;15", 112403, 0},
{"df crossing the first dropped label", "00:00:59;29", "00:00:00;01", "29.97df", "00:01:00;02", 1800, 0},
{"df crossing into a tenth minute", "00:09:59;29", "00:00:00;01", "29.97df", "00:10:00;00", 17982, 0},
{"df crossing minute eleven", "00:10:59;29", "00:00:00;01", "29.97df", "00:11:00;02", 19782, 0},
{"df59 crossing the first dropped label", "00:00:59;59", "00:00:00;01", "59.94df", "00:01:00;04", 3600, 0},
{"df adding a whole hour", "01:00:00;00", "01:00:00;00", "29.97df", "02:00:00;00", 215784, 0},
{"25 simple", "01:00:00:00", "00:00:10:00", "25", "01:00:10:00", 90250, 0},
{"24 carry into seconds", "00:00:00:23", "00:00:00:01", "24", "00:00:01:00", 24, 0},
{"30 carry into minutes", "00:00:59:29", "00:00:00:01", "30", "00:01:00:00", 1800, 0},
{"25 rollover at midnight", "23:59:59:24", "00:00:00:01", "25", "00:00:00:00", 2160000, 1},
{"df rollover at midnight", "23:59:59;29", "00:00:00;01", "29.97df", "00:00:00;00", 2589408, 1},
{"df59 rollover at midnight", "23:59:59;59", "00:00:00;01", "59.94df", "00:00:00;00", 5178816, 1},
{"24 rollover from two halves", "12:00:00:00", "12:00:00:00", "24", "00:00:00:00", 2073600, 1},
{"30 rollover with remainder", "20:00:00:00", "10:00:00:00", "30", "06:00:00:00", 3240000, 1},
{"60 rollover with remainder", "23:00:00:00", "02:00:00:00", "60", "01:00:00:00", 5400000, 1},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
a := mustParse(t, c.a, c.rate)
b := mustParse(t, c.b, c.rate)
sum, err := a.Add(b)
if err != nil {
t.Fatalf("Add: %v", err)
}
if sum.Frames() != c.frames {
t.Fatalf("%s + %s at %s = %d frames, want %d", c.a, c.b, c.rate, sum.Frames(), c.frames)
}
wrapped, days := sum.Wrap()
if days != c.days {
t.Fatalf("%s + %s at %s wrapped by %d days, want %d", c.a, c.b, c.rate, days, c.days)
}
shown := sum
if days != 0 {
shown = wrapped
}
if shown.String() != c.want {
t.Fatalf("%s + %s at %s = %s, want %s", c.a, c.b, c.rate, shown, c.want)
}
})
}
}
func TestSub(t *testing.T) {
cases := []struct {
name string
a, b string
rate string
want string
frames int
}{
{"df hour minus two and a half minutes", "01:00:00;00", "00:02:30;15", "29.97df", "00:57:29;15", 103381},
{"df stepping back over a dropped label", "00:01:00;02", "00:00:00;01", "29.97df", "00:00:59;29", 1799},
{"df stepping back into a tenth minute", "00:10:00;00", "00:00:00;01", "29.97df", "00:09:59;29", 17981},
{"df59 stepping back over a dropped label", "00:01:00;04", "00:00:00;01", "59.94df", "00:00:59;59", 3599},
{"25 to zero", "00:00:10:00", "00:00:10:00", "25", "00:00:00:00", 0},
{"25 one frame negative", "00:00:00:00", "00:00:00:01", "25", "-00:00:00:01", -1},
{"25 one second negative", "00:00:00:00", "00:00:01:00", "25", "-00:00:01:00", -25},
{"df one second negative", "00:00:00;00", "00:00:01;00", "29.97df", "-00:00:01;00", -30},
{"24 one hour negative", "01:00:00:00", "02:00:00:00", "24", "-01:00:00:00", -86400},
{"df hour negative", "00:00:00;00", "01:00:00;00", "29.97df", "-01:00:00;00", -107892},
{"30 borrow across a minute", "00:01:00:00", "00:00:00:01", "30", "00:00:59:29", 1799},
{"60 borrow across an hour", "01:00:00:00", "00:00:00:01", "60", "00:59:59:59", 215999},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
a := mustParse(t, c.a, c.rate)
b := mustParse(t, c.b, c.rate)
diff, err := a.Sub(b)
if err != nil {
t.Fatalf("Sub: %v", err)
}
if diff.Frames() != c.frames {
t.Fatalf("%s - %s at %s = %d frames, want %d", c.a, c.b, c.rate, diff.Frames(), c.frames)
}
if diff.String() != c.want {
t.Fatalf("%s - %s at %s = %s, want %s", c.a, c.b, c.rate, diff, c.want)
}
})
}
}
// TestNegativeRoundTrip checks that a negative result can be read back in.
func TestNegativeRoundTrip(t *testing.T) {
cases := []struct {
name string
frames int
rate string
}{
{"25 one frame", -1, "25"},
{"25 one hour", -90000, "25"},
{"24 one second", -24, "24"},
{"df one hour", -107892, "29.97df"},
{"df one minute", -1800, "29.97df"},
{"df59 one hour", -215784, "59.94df"},
{"29.97 one hour", -108000, "29.97"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := mustRate(t, c.rate)
text := FromFrames(c.frames, r).String()
back, err := Parse(text, r)
if err != nil {
t.Fatalf("Parse(%q, %s): %v", text, c.rate, err)
}
if back.Frames() != c.frames {
t.Fatalf("round trip of %d at %s gave %d through %q", c.frames, c.rate, back.Frames(), text)
}
})
}
}
func TestWrap(t *testing.T) {
cases := []struct {
name string
frames int
rate string
want string
days int
}{
{"df exactly one day", 2589408, "29.97df", "00:00:00;00", 1},
{"df one frame past midnight", 2589409, "29.97df", "00:00:00;01", 1},
{"df two days", 5178816, "29.97df", "00:00:00;00", 2},
{"df one frame before zero", -1, "29.97df", "23:59:59;29", -1},
{"df one hour before zero", -107892, "29.97df", "23:00:00;00", -1},
{"25 exactly one day", 2160000, "25", "00:00:00:00", 1},
{"25 two days", 4320000, "25", "00:00:00:00", 2},
{"25 one frame before zero", -1, "25", "23:59:59:24", -1},
{"25 one hour before zero", -90000, "25", "23:00:00:00", -1},
{"24 one day", 2073600, "24", "00:00:00:00", 1},
{"60 one day", 5184000, "60", "00:00:00:00", 1},
{"df59 one day", 5178816, "59.94df", "00:00:00;00", 1},
{"inside the day is untouched", 90000, "25", "01:00:00:00", 0},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, days := FromFrames(c.frames, mustRate(t, c.rate)).Wrap()
if days != c.days {
t.Fatalf("Wrap(%d at %s) days = %d, want %d", c.frames, c.rate, days, c.days)
}
if got.String() != c.want {
t.Fatalf("Wrap(%d at %s) = %s, want %s", c.frames, c.rate, got, c.want)
}
})
}
}
func TestConvert(t *testing.T) {
cases := []struct {
name string
in string
from, to string
want string
wantFrames int
}{
{"df hour to non drop 29.97", "01:00:00;00", "29.97df", "29.97", "00:59:56:12", 107892},
{"non drop 29.97 hour to df", "01:00:00:00", "29.97", "29.97df", "01:00:03;18", 108000},
{"df hour to 23.976", "01:00:00;00", "29.97df", "23.976", "00:59:56:10", 86314},
{"df hour to 30", "01:00:00;00", "29.97df", "30", "01:00:00:00", 108000},
{"df hour to 59.94df", "01:00:00;00", "29.97df", "59.94df", "01:00:00;00", 215784},
{"59.94df hour to 29.97df", "01:00:00;00", "59.94df", "29.97df", "01:00:00;00", 107892},
{"24 to 25", "01:00:00:00", "24", "25", "01:00:00:00", 90000},
{"25 to 24", "01:00:00:00", "25", "24", "01:00:00:00", 86400},
{"25 to 50", "01:00:00:00", "25", "50", "01:00:00:00", 180000},
{"50 to 25", "01:00:00:00", "50", "25", "01:00:00:00", 90000},
{"23.976 to 24", "01:00:00:00", "23.976", "24", "01:00:03:14", 86486},
{"24 to 23.976", "01:00:00:00", "24", "23.976", "00:59:56:10", 86314},
{"30 to 60", "01:00:00:00", "30", "60", "01:00:00:00", 216000},
{"zero converts to zero", "00:00:00:00", "25", "29.97df", "00:00:00;00", 0},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
src := mustParse(t, c.in, c.from)
dst := src.ConvertTo(mustRate(t, c.to))
if dst.Frames() != c.wantFrames {
t.Fatalf("%s at %s to %s = %d frames, want %d", c.in, c.from, c.to, dst.Frames(), c.wantFrames)
}
if dst.String() != c.want {
t.Fatalf("%s at %s to %s = %s, want %s", c.in, c.from, c.to, dst, c.want)
}
})
}
}
// TestNanos checks the real elapsed time of a position. The 1000/1001 rates
// are the interesting rows.
func TestNanos(t *testing.T) {
cases := []struct {
name string
in string
rate string
want int64
}{
{"df hour is 3599.9964 s", "01:00:00;00", "29.97df", 3599996400000},
{"non drop 29.97 hour is 3603.6 s", "01:00:00:00", "29.97", 3603600000000},
{"23.976 hour is 3603.6 s", "01:00:00:00", "23.976", 3603600000000},
{"59.94 hour is 3603.6 s", "01:00:00:00", "59.94", 3603600000000},
{"59.94df hour is 3599.9964 s", "01:00:00;00", "59.94df", 3599996400000},
{"24 hour is exact", "01:00:00:00", "24", 3600000000000},
{"25 hour is exact", "01:00:00:00", "25", 3600000000000},
{"30 hour is exact", "01:00:00:00", "30", 3600000000000},
{"50 hour is exact", "01:00:00:00", "50", 3600000000000},
{"60 hour is exact", "01:00:00:00", "60", 3600000000000},
{"df ten minutes", "00:10:00;00", "29.97df", 599999400000},
{"25 ten seconds", "00:00:10:00", "25", 10000000000},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := mustParse(t, c.in, c.rate).Nanos()
if got != c.want {
t.Fatalf("Nanos(%s at %s) = %d, want %d", c.in, c.rate, got, c.want)
}
})
}
}
// TestDrift checks the gap between the timecode label and the wall clock. A
// positive value means the label reads later than real time.
func TestDrift(t *testing.T) {
cases := []struct {
name string
in string
rate string
want int64
}{
{"df hour gains 3.6 ms", "01:00:00;00", "29.97df", 3600000},
{"df ten minutes gains 0.6 ms", "00:10:00;00", "29.97df", 600000},
{"df 23 hours gains 82.8 ms", "23:00:00;00", "29.97df", 82800000},
{"df zero has no drift", "00:00:00;00", "29.97df", 0},
{"df59 hour gains 3.6 ms", "01:00:00;00", "59.94df", 3600000},
{"non drop 29.97 hour loses 3.6 s", "01:00:00:00", "29.97", -3600000000},
{"non drop 29.97 two hours loses 7.2 s", "02:00:00:00", "29.97", -7200000000},
{"23.976 hour loses 3.6 s", "01:00:00:00", "23.976", -3600000000},
{"59.94 hour loses 3.6 s", "01:00:00:00", "59.94", -3600000000},
{"24 never drifts", "01:00:00:00", "24", 0},
{"25 never drifts", "05:00:00:00", "25", 0},
{"30 never drifts", "12:34:56:07", "30", 0},
{"50 never drifts", "23:59:59:49", "50", 0},
{"60 never drifts", "01:00:00:00", "60", 0},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := mustParse(t, c.in, c.rate).DriftNanos()
if got != c.want {
t.Fatalf("DriftNanos(%s at %s) = %d, want %d", c.in, c.rate, got, c.want)
}
})
}
}
func TestDriftPerHourAndDay(t *testing.T) {
cases := []struct {
rate string
perHour int64
perDay int64
exactRat bool
}{
{"23.976", -3600000000, -86400000000, false},
{"24", 0, 0, true},
{"25", 0, 0, true},
{"29.97", -3600000000, -86400000000, false},
{"29.97df", 3600000, 86400000, false},
{"30", 0, 0, true},
{"50", 0, 0, true},
{"59.94", -3600000000, -86400000000, false},
{"59.94df", 3600000, 86400000, false},
{"60", 0, 0, true},
}
for _, c := range cases {
t.Run(c.rate, func(t *testing.T) {
r := mustRate(t, c.rate)
if got := r.DriftPerHourNanos(); got != c.perHour {
t.Fatalf("DriftPerHourNanos(%s) = %d, want %d", c.rate, got, c.perHour)
}
if got := r.DriftPerDayNanos(); got != c.perDay {
t.Fatalf("DriftPerDayNanos(%s) = %d, want %d", c.rate, got, c.perDay)
}
})
}
}
// TestRange covers the duration between two positions.
func TestRange(t *testing.T) {
cases := []struct {
name string
a, b string
rate string
frames int
seconds int64
want string
}{
{"25 ten seconds", "01:00:00:00", "01:00:10:00", "25", 250, 10000000000, "00:00:10:00"},
{"25 backwards", "01:00:10:00", "01:00:00:00", "25", -250, -10000000000, "-00:00:10:00"},
{"df across a dropped label", "00:00:59;29", "00:01:00;02", "29.97df", 1, 33366666, "00:00:00;01"},
{"df one hour", "01:00:00;00", "02:00:00;00", "29.97df", 107892, 3599996400000, "01:00:00;00"},
{"24 one frame", "00:00:00:00", "00:00:00:01", "24", 1, 41666666, "00:00:00:01"},
{"60 one minute", "00:00:00:00", "00:01:00:00", "60", 3600, 60000000000, "00:01:00:00"},
{"empty range", "01:00:00:00", "01:00:00:00", "30", 0, 0, "00:00:00:00"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
a := mustParse(t, c.a, c.rate)
b := mustParse(t, c.b, c.rate)
span, err := b.Sub(a)
if err != nil {
t.Fatalf("Sub: %v", err)
}
if span.Frames() != c.frames {
t.Fatalf("range %s to %s at %s = %d frames, want %d", c.a, c.b, c.rate, span.Frames(), c.frames)
}
if span.Nanos() != c.seconds {
t.Fatalf("range %s to %s at %s = %d ns, want %d", c.a, c.b, c.rate, span.Nanos(), c.seconds)
}
if span.String() != c.want {
t.Fatalf("range %s to %s at %s = %s, want %s", c.a, c.b, c.rate, span, c.want)
}
})
}
}
// TestRateMismatch checks that arithmetic across rates is refused rather than
// silently producing a wrong answer.
func TestRateMismatch(t *testing.T) {
a := mustParse(t, "01:00:00;00", "29.97df")
b := mustParse(t, "01:00:00:00", "29.97")
t.Run("add", func(t *testing.T) {
if _, err := a.Add(b); err == nil {
t.Fatal("Add across rates should fail")
}
})
t.Run("sub", func(t *testing.T) {
if _, err := a.Sub(b); err == nil {
t.Fatal("Sub across rates should fail")
}
})
}
// TestLabelAccounting checks the label bookkeeping that drop frame rests on.
func TestLabelAccounting(t *testing.T) {
cases := []struct {
name string
in string
rate string
labels int
dropped int
frames int
}{
{"df hour", "01:00:00;00", "29.97df", 108000, 108, 107892},
{"df ten minutes", "00:10:00;00", "29.97df", 18000, 18, 17982},
{"df one minute", "00:01:00;02", "29.97df", 1802, 2, 1800},
{"df first minute", "00:00:59;29", "29.97df", 1799, 0, 1799},
{"df day", "23:59:59;29", "29.97df", 2591999, 2592, 2589407},
{"df59 hour", "01:00:00;00", "59.94df", 216000, 216, 215784},
{"df59 ten minutes", "00:10:00;00", "59.94df", 36000, 36, 35964},
{"non drop 29.97 hour", "01:00:00:00", "29.97", 108000, 0, 108000},
{"25 hour", "01:00:00:00", "25", 90000, 0, 90000},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
tc := mustParse(t, c.in, c.rate)
if tc.LabelIndex() != c.labels {
t.Errorf("LabelIndex = %d, want %d", tc.LabelIndex(), c.labels)
}
if tc.DroppedLabels() != c.dropped {
t.Errorf("DroppedLabels = %d, want %d", tc.DroppedLabels(), c.dropped)
}
if tc.Frames() != c.frames {
t.Errorf("Frames = %d, want %d", tc.Frames(), c.frames)
}
})
}
}
// TestEveryFrameRoundTrip walks frame counts through String and back through
// Parse. Every drop frame position in a whole day is checked, which also
// proves that the formatter never produces a label that drop frame skips,
// because Parse refuses those. The other rates are sampled.
func TestEveryFrameRoundTrip(t *testing.T) {
if testing.Short() {
t.Skip("skipping the whole day walk in short mode")
}
cases := []struct {
rate string
step int
}{
{"23.976", 97},
{"24", 97},
{"25", 97},
{"29.97", 97},
{"29.97df", 1},
{"30", 97},
{"50", 97},
{"59.94", 97},
{"59.94df", 1},
{"60", 97},
}
for _, c := range cases {
t.Run(c.rate, func(t *testing.T) {
r := mustRate(t, c.rate)
day := r.FramesPerDay()
previous := -1
for n := 0; n < day; n += c.step {
text := FromFrames(n, r).String()
back, err := Parse(text, r)
if err != nil {
t.Fatalf("frame %d printed as %q which does not parse: %v", n, text, err)
}
if back.Frames() != n {
t.Fatalf("frame %d printed as %q parsed back as %d", n, text, back.Frames())
}
if c.step == 1 {
if n > 0 && text <= FromFrames(previous, r).String() {
t.Fatalf("frame %d printed as %q which does not sort after frame %d", n, text, previous)
}
previous = n
}
}
})
}
}
// TestDropFrameMinuteBoundaries checks the label just before and just after
// every minute boundary in an hour, which is where drop frame goes wrong.
func TestDropFrameMinuteBoundaries(t *testing.T) {
for _, rateName := range []string{"29.97df", "59.94df"} {
t.Run(rateName, func(t *testing.T) {
r := mustRate(t, rateName)
for minute := 1; minute < 60; minute++ {
wantF := r.DropCount()
if minute%10 == 0 {
wantF = 0
}
start := r.labelIndex(0, minute, 0, wantF) - r.droppedBefore(0, minute)
firstOfMinute := FromFrames(start, r)
lastOfPrevious := FromFrames(start-1, r)
_, prevM, prevS, prevF, _ := lastOfPrevious.Fields()
if prevM != minute-1 || prevS != 59 || prevF != r.Nominal()-1 {
t.Fatalf("frame before minute %d is %s, want the last frame of minute %d",
minute, lastOfPrevious, minute-1)
}
_, gotM, gotS, gotF, _ := firstOfMinute.Fields()
if gotM != minute || gotS != 0 || gotF != wantF {
t.Fatalf("first frame of minute %d is %s, want frame %02d", minute, firstOfMinute, wantF)
}
}
})
}
}
// TestAddFramesAndDuration covers the small helpers on the public surface.
func TestAddFramesAndDuration(t *testing.T) {
cases := []struct {
name string
in string
rate string
step int
want string
wantDur time.Duration
}{
{"df forward over a dropped label", "00:00:59;29", "29.97df", 1, "00:01:00;02", 60060 * time.Millisecond},
{"df back over a dropped label", "00:01:00;02", "29.97df", -1, "00:00:59;29", 60026633333 * time.Nanosecond},
{"25 forward one second", "00:00:00:00", "25", 25, "00:00:01:00", time.Second},
{"25 back past zero", "00:00:00:00", "25", -1, "-00:00:00:01", -40 * time.Millisecond},
{"24 forward an hour", "00:00:00:00", "24", 86400, "01:00:00:00", time.Hour},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := mustParse(t, c.in, c.rate).AddFrames(c.step)
if got.String() != c.want {
t.Fatalf("AddFrames(%d) on %s at %s = %s, want %s", c.step, c.in, c.rate, got, c.want)
}
if got.Duration() != c.wantDur {
t.Fatalf("Duration of %s at %s = %v, want %v", got, c.rate, got.Duration(), c.wantDur)
}
})
}
}
// TestRatesListing checks that the exported rate list matches the names.
func TestRatesListing(t *testing.T) {
names := RateNames()
rates := Rates()
if len(names) != len(rates) || len(names) != 10 {
t.Fatalf("RateNames has %d entries and Rates has %d, want 10 of each", len(names), len(rates))
}
for i, r := range rates {
t.Run(names[i], func(t *testing.T) {
if r.String() != names[i] {
t.Fatalf("Rates()[%d] = %s, want %s", i, r, names[i])
}
if got, err := ParseRate(names[i]); err != nil || got != r {
t.Fatalf("ParseRate(%q) = %v, %v, want %v", names[i], got, err, r)
}
if r.FPS() <= 0 {
t.Fatalf("FPS of %s is %v", r, r.FPS())
}
})
}
}
// TestKnownConstants pins the numbers that show up in the literature.
func TestKnownConstants(t *testing.T) {
checks := []struct {
name string
got int
want int
}{
{"one hour of 29.97df is 107892 frames", mustParse(t, "01:00:00;00", "29.97df").Frames(), 107892},
{"00:01:00;02 at 29.97df is frame 1800", mustParse(t, "00:01:00;02", "29.97df").Frames(), 1800},
{"00:10:00;00 at 29.97df is frame 17982", mustParse(t, "00:10:00;00", "29.97df").Frames(), 17982},
{"one day of 29.97df is 2589408 frames", mustRate(t, "29.97df").FramesPerDay(), 2589408},
{"one hour of 29.97df drops 108 labels", mustParse(t, "01:00:00;00", "29.97df").DroppedLabels(), 108},
{"one day of 29.97df drops 2592 labels", mustParse(t, "23:59:59;29", "29.97df").DroppedLabels(), 2592},
{"one hour of 59.94df is 215784 frames", mustParse(t, "01:00:00;00", "59.94df").Frames(), 215784},
{"one hour of 59.94df drops 216 labels", mustParse(t, "01:00:00;00", "59.94df").DroppedLabels(), 216},
{"one hour of non drop 29.97 is 108000 frames", mustParse(t, "01:00:00:00", "29.97").Frames(), 108000},
}
for _, c := range checks {
t.Run(c.name, func(t *testing.T) {
if c.got != c.want {
t.Fatalf("got %d, want %d", c.got, c.want)
}
})
}
}