| 1 |
package magic |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
func BenchmarkJSONPathological(b *testing.B) { |
| 10 |
const n = 1000 |
| 11 |
hugeArray := []byte( |
| 12 |
strings.Repeat("[1,", n) + |
| 13 |
`2,3,"abc",true,false,null` + |
| 14 |
strings.Repeat("]", n)) |
| 15 |
hugeObject := []byte( |
| 16 |
strings.Repeat(`{"a": 1, "b":`, n) + |
| 17 |
`{"c":[2,3,"abc",true,false,null]}` + |
| 18 |
strings.Repeat("}", n)) |
| 19 |
|
| 20 |
b.ReportAllocs() |
| 21 |
for b.Loop() { |
| 22 |
if !JSON(hugeArray, 0) { |
| 23 |
b.Fatal("huge array should be JSON") |
| 24 |
} |
| 25 |
if !JSON(hugeObject, 0) { |
| 26 |
b.Fatal("huge object should be JSON") |
| 27 |
} |
| 28 |
GeoJSON(hugeArray, 0) |
| 29 |
GeoJSON(hugeObject, 0) |
| 30 |
HAR(hugeArray, 0) |
| 31 |
HAR(hugeObject, 0) |
| 32 |
GLTF(hugeArray, 0) |
| 33 |
GLTF(hugeObject, 0) |
| 34 |
NdJSON(hugeArray, 0) |
| 35 |
NdJSON(hugeObject, 0) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestRFC822(t *testing.T) { |
| 40 |
testcases := []struct { |
| 41 |
name string |
| 42 |
in string |
| 43 |
expected bool |
| 44 |
}{{ |
| 45 |
"empty", "", false, |
| 46 |
}, { |
| 47 |
"one hint", "Cc: [email protected]", false, |
| 48 |
}, { |
| 49 |
"two identical hints", "Cc: [email protected]\nCc: [email protected]", true, |
| 50 |
}, { |
| 51 |
"two different hints", "Cc: [email protected]\nTo: [email protected]", true, |
| 52 |
}, { |
| 53 |
"junk at start", "junk\nCc: [email protected]\nTo: [email protected]", false, |
| 54 |
}, { |
| 55 |
"junk later", "Cc: [email protected]\njunk To: [email protected]", false, |
| 56 |
}} |
| 57 |
|
| 58 |
for _, tc := range testcases { |
| 59 |
t.Run(tc.name, func(t *testing.T) { |
| 60 |
got := RFC822([]byte(tc.in), 0) |
| 61 |
if tc.expected != got { |
| 62 |
t.Errorf("expected: %t, got: %t", tc.expected, got) |
| 63 |
} |
| 64 |
}) |
| 65 |
} |
| 66 |
} |