c: ICE with invalid sizeof [PR115642]
[official-gcc.git] / libgo / go / mime / type_test.go
blob5769c6a55de294d6405c767785049ae604109d28
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package mime
7 import (
8 "reflect"
9 "strings"
10 "sync"
11 "testing"
14 func setMimeInit(fn func()) (cleanup func()) {
15 once = sync.Once{}
16 testInitMime = fn
17 return func() {
18 testInitMime = nil
19 once = sync.Once{}
23 func clearMimeTypes() {
24 setMimeTypes(map[string]string{}, map[string]string{})
27 func setType(ext, typ string) {
28 if !strings.HasPrefix(ext, ".") {
29 panic("missing leading dot")
31 if err := setExtensionType(ext, typ); err != nil {
32 panic("bad test data: " + err.Error())
36 func TestTypeByExtension(t *testing.T) {
37 once = sync.Once{}
38 // initMimeForTests returns the platform-specific extension =>
39 // type tests. On Unix and Plan 9, this also tests the parsing
40 // of MIME text files (in testdata/*). On Windows, we test the
41 // real registry on the machine and assume that ".png" exists
42 // there, which empirically it always has, for all versions of
43 // Windows.
44 typeTests := initMimeForTests()
46 for ext, want := range typeTests {
47 val := TypeByExtension(ext)
48 if val != want {
49 t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want)
54 func TestTypeByExtension_LocalData(t *testing.T) {
55 cleanup := setMimeInit(func() {
56 clearMimeTypes()
57 setType(".foo", "x/foo")
58 setType(".bar", "x/bar")
59 setType(".Bar", "x/bar; capital=1")
61 defer cleanup()
63 tests := map[string]string{
64 ".foo": "x/foo",
65 ".bar": "x/bar",
66 ".Bar": "x/bar; capital=1",
67 ".sdlkfjskdlfj": "",
68 ".t1": "", // testdata shouldn't be used
71 for ext, want := range tests {
72 val := TypeByExtension(ext)
73 if val != want {
74 t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want)
79 func TestTypeByExtensionCase(t *testing.T) {
80 const custom = "test/test; charset=iso-8859-1"
81 const caps = "test/test; WAS=ALLCAPS"
83 cleanup := setMimeInit(func() {
84 clearMimeTypes()
85 setType(".TEST", caps)
86 setType(".tesT", custom)
88 defer cleanup()
90 // case-sensitive lookup
91 if got := TypeByExtension(".tesT"); got != custom {
92 t.Fatalf("for .tesT, got %q; want %q", got, custom)
94 if got := TypeByExtension(".TEST"); got != caps {
95 t.Fatalf("for .TEST, got %q; want %s", got, caps)
98 // case-insensitive
99 if got := TypeByExtension(".TesT"); got != custom {
100 t.Fatalf("for .TesT, got %q; want %q", got, custom)
104 func TestExtensionsByType(t *testing.T) {
105 cleanup := setMimeInit(func() {
106 clearMimeTypes()
107 setType(".gif", "image/gif")
108 setType(".a", "foo/letter")
109 setType(".b", "foo/letter")
110 setType(".B", "foo/letter")
111 setType(".PNG", "image/png")
113 defer cleanup()
115 tests := []struct {
116 typ string
117 want []string
118 wantErr string
120 {typ: "image/gif", want: []string{".gif"}},
121 {typ: "image/png", want: []string{".png"}}, // lowercase
122 {typ: "foo/letter", want: []string{".a", ".b"}},
123 {typ: "x/unknown", want: nil},
126 for _, tt := range tests {
127 got, err := ExtensionsByType(tt.typ)
128 if err != nil && tt.wantErr != "" && strings.Contains(err.Error(), tt.wantErr) {
129 continue
131 if err != nil {
132 t.Errorf("ExtensionsByType(%q) error: %v", tt.typ, err)
133 continue
135 if tt.wantErr != "" {
136 t.Errorf("ExtensionsByType(%q) = %q, %v; want error substring %q", tt.typ, got, err, tt.wantErr)
137 continue
139 if !reflect.DeepEqual(got, tt.want) {
140 t.Errorf("ExtensionsByType(%q) = %q; want %q", tt.typ, got, tt.want)
145 func TestLookupMallocs(t *testing.T) {
146 t.Skip("skipping test on gccgo until it has better escape analysis")
147 n := testing.AllocsPerRun(10000, func() {
148 TypeByExtension(".html")
149 TypeByExtension(".HtML")
151 if n > 0 {
152 t.Errorf("allocs = %v; want 0", n)
156 func BenchmarkTypeByExtension(b *testing.B) {
157 initMime()
158 b.ResetTimer()
160 for _, ext := range []string{
161 ".html",
162 ".HTML",
163 ".unused",
165 b.Run(ext, func(b *testing.B) {
166 b.RunParallel(func(pb *testing.PB) {
167 for pb.Next() {
168 TypeByExtension(ext)
175 func BenchmarkExtensionsByType(b *testing.B) {
176 initMime()
177 b.ResetTimer()
179 for _, typ := range []string{
180 "text/html",
181 "text/html; charset=utf-8",
182 "application/octet-stream",
184 b.Run(typ, func(b *testing.B) {
185 b.RunParallel(func(pb *testing.PB) {
186 for pb.Next() {
187 if _, err := ExtensionsByType(typ); err != nil {
188 b.Fatal(err)
196 func TestExtensionsByType2(t *testing.T) {
197 cleanup := setMimeInit(func() {
198 clearMimeTypes()
199 // Initialize built-in types like in type.go before osInitMime.
200 setMimeTypes(builtinTypesLower, builtinTypesLower)
202 defer cleanup()
204 tests := []struct {
205 typ string
206 want []string
208 {typ: "image/jpeg", want: []string{".jpeg", ".jpg"}},
211 for _, tt := range tests {
212 got, err := ExtensionsByType(tt.typ)
213 if err != nil {
214 t.Errorf("ExtensionsByType(%q): %v", tt.typ, err)
215 continue
217 if !reflect.DeepEqual(got, tt.want) {
218 t.Errorf("ExtensionsByType(%q) = %q; want %q", tt.typ, got, tt.want)