-
Notifications
You must be signed in to change notification settings - Fork 122
/
lilliput_test.go
119 lines (114 loc) · 3.07 KB
/
lilliput_test.go
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
// Package lilliput resizes and encodes images from
// compressed images
package lilliput
import (
"io/ioutil"
"testing"
)
func TestNewDecoder(t *testing.T) {
tests := []struct {
name string
sourceFilePath string
wantHeight int
wantWidth int
wantErr bool
wantNegativeDuration bool
}{
{
name: "Standard MP4",
sourceFilePath: "testdata/big_buck_bunny_480p_10s_std.mp4",
wantHeight: 480,
wantWidth: 720,
wantErr: false,
},
{
name: "Progressive Download MP4",
sourceFilePath: "testdata/big_buck_bunny_480p_10s_web.mp4",
wantHeight: 480,
wantWidth: 720,
wantErr: false,
},
{
name: "Audio-only MP3",
sourceFilePath: "testdata/tos-intro-3s.mp3",
wantErr: false,
},
{
name: "Audio-only OGG",
sourceFilePath: "testdata/tos-intro-3s.ogg",
wantErr: false,
},
{
name: "Audio-only AAC",
sourceFilePath: "testdata/tos-intro-3s.aac",
wantErr: false,
wantNegativeDuration: true,
},
{
name: "Audio-only FLAC",
sourceFilePath: "testdata/tos-intro-3s.flac",
wantErr: false,
},
{
name: "Audio-only WAV",
sourceFilePath: "testdata/tos-intro-3s.wav",
wantErr: false,
},
{
name: "WebP Image",
sourceFilePath: "testdata/tears_of_steel_icc.webp",
wantWidth: 1920,
wantHeight: 800,
wantNegativeDuration: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sourceFileData, err := ioutil.ReadFile(tt.sourceFilePath)
if err != nil {
t.Fatalf("Failed to read source file: %v", err)
}
dec, err := NewDecoder(sourceFileData)
if (err != nil) != tt.wantErr {
t.Errorf("NewDecoder() error = %v, wantErr %v", err, tt.wantErr)
return
}
header, err := dec.Header()
if err != nil {
t.Errorf("Failed to get header: %v", err)
}
if header.Width() != tt.wantWidth {
t.Errorf("Expected width to be %v, got %v", tt.wantWidth, header.Width())
}
if header.Height() != tt.wantHeight {
t.Errorf("Expected height to be %v, got %v", tt.wantHeight, header.Height())
}
if tt.wantNegativeDuration && dec.Duration() > 0 {
t.Errorf("Expected duration to be less than 0, got %v", dec.Duration())
}
if !tt.wantNegativeDuration && dec.Duration() <= 0 {
t.Errorf("Expected duration to be greater than 0, got %v", dec.Duration())
}
})
}
}
func BenchmarkNewDecoder(b *testing.B) {
sourceFilePath := "testdata/big_buck_bunny_480p_10s_web.mp4"
sourceFileData, err := ioutil.ReadFile(sourceFilePath)
if err != nil {
b.Fatalf("Failed to read source file: %v", err)
}
b.ResetTimer() // Start timing after setup
for i := 0; i < b.N; i++ {
dec, err := NewDecoder(sourceFileData)
if err != nil {
b.Fatalf("Failed to create decoder: %v", err)
}
if dec != nil {
defer dec.Close()
}
if _, err := dec.Header(); err != nil {
b.Fatalf("Failed to get header: %v", err)
}
}
}