-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
167 lines (138 loc) · 4.55 KB
/
Copy pathapi_test.go
File metadata and controls
167 lines (138 loc) · 4.55 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
package ansort
import (
"testing"
"time"
)
// TestAPISimplification verifies that the standard API now uses optimized implementations
func TestAPISimplification(t *testing.T) {
testData := []string{
"file1.txt", "file10.txt", "file2.txt", "file20.txt", "file3.txt",
"doc1.pdf", "doc10.pdf", "doc2.pdf", "doc20.pdf", "doc3.pdf",
"image1.jpg", "image10.jpg", "image2.jpg", "image20.jpg", "image3.jpg",
}
t.Run("SortStrings uses optimized implementation", func(t *testing.T) {
data := make([]string, len(testData))
copy(data, testData)
// Time the standard SortStrings function
start := time.Now()
SortStrings(data)
duration := time.Since(start)
// Verify it's sorted correctly
expected := []string{
"doc1.pdf", "doc2.pdf", "doc3.pdf", "doc10.pdf", "doc20.pdf",
"file1.txt", "file2.txt", "file3.txt", "file10.txt", "file20.txt",
"image1.jpg", "image2.jpg", "image3.jpg", "image10.jpg", "image20.jpg",
}
if len(data) != len(expected) {
t.Fatalf("Length mismatch: got %d, want %d", len(data), len(expected))
}
for i, v := range data {
if v != expected[i] {
t.Errorf("Position %d: got %q, want %q", i, v, expected[i])
}
}
t.Logf("SortStrings completed in %v", duration)
})
t.Run("Compare uses optimized implementation", func(t *testing.T) {
// Test basic comparison
result := Compare("file1.txt", "file10.txt")
if result != -1 {
t.Errorf("Compare(file1.txt, file10.txt): got %d, want -1", result)
}
result = Compare("file10.txt", "file2.txt")
if result != 1 {
t.Errorf("Compare(file10.txt, file2.txt): got %d, want 1", result)
}
result = Compare("file1.txt", "file1.txt")
if result != 0 {
t.Errorf("Compare(file1.txt, file1.txt): got %d, want 0", result)
}
})
t.Run("Cache control functions work", func(t *testing.T) {
// Test disabling cache
DisableCache()
data := make([]string, len(testData))
copy(data, testData)
SortStrings(data) // Should use legacy implementation
// Verify it still works correctly
expected := []string{
"doc1.pdf", "doc2.pdf", "doc3.pdf", "doc10.pdf", "doc20.pdf",
"file1.txt", "file2.txt", "file3.txt", "file10.txt", "file20.txt",
"image1.jpg", "image2.jpg", "image3.jpg", "image10.jpg", "image20.jpg",
}
for i, v := range data {
if v != expected[i] {
t.Errorf("Position %d: got %q, want %q", i, v, expected[i])
}
}
// Re-enable cache
EnableCache()
// Test configuring cache size
ConfigureCacheSize(5000)
// Verify comparison still works
result := Compare("file1.txt", "file10.txt")
if result != -1 {
t.Errorf("After cache reconfiguration, Compare(file1.txt, file10.txt): got %d, want -1", result)
}
// Reset cache size to default for other tests
ConfigureCacheSize(2000)
})
t.Run("Legacy functions still work", func(t *testing.T) {
data := make([]string, len(testData))
copy(data, testData)
// Test legacy functions
SortStringsLegacy(data)
expected := []string{
"doc1.pdf", "doc2.pdf", "doc3.pdf", "doc10.pdf", "doc20.pdf",
"file1.txt", "file2.txt", "file3.txt", "file10.txt", "file20.txt",
"image1.jpg", "image2.jpg", "image3.jpg", "image10.jpg", "image20.jpg",
}
for i, v := range data {
if v != expected[i] {
t.Errorf("Legacy sort position %d: got %q, want %q", i, v, expected[i])
}
}
// Test legacy compare
result := CompareLegacy("file1.txt", "file10.txt")
if result != -1 {
t.Errorf("CompareLegacy(file1.txt, file10.txt): got %d, want -1", result)
}
})
}
// BenchmarkAPIPerformance verifies that the default API provides optimized performance
func BenchmarkAPIPerformance(t *testing.B) {
testData := []string{
"file1.txt", "file10.txt", "file2.txt", "file20.txt", "file3.txt",
"doc1.pdf", "doc10.pdf", "doc2.pdf", "doc20.pdf", "doc3.pdf",
"image1.jpg", "image10.jpg", "image2.jpg", "image20.jpg", "image3.jpg",
"photo100.png", "photo11.png", "photo2.png", "photo20.png", "photo3.png",
}
t.Run("SortStrings_Default", func(b *testing.B) {
for i := 0; i < b.N; i++ {
data := make([]string, len(testData))
copy(data, testData)
SortStrings(data)
}
})
t.Run("SortStrings_Legacy", func(b *testing.B) {
for i := 0; i < b.N; i++ {
data := make([]string, len(testData))
copy(data, testData)
SortStringsLegacy(data)
}
})
t.Run("Compare_Default", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < len(testData)-1; j++ {
Compare(testData[j], testData[j+1])
}
}
})
t.Run("Compare_Legacy", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < len(testData)-1; j++ {
CompareLegacy(testData[j], testData[j+1])
}
}
})
}