| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/mailru/easyjson" |
| 7 |
) |
| 8 |
|
| 9 |
func TestStringIntern(t *testing.T) { |
| 10 |
data := []byte(`{"field": "string interning test"}`) |
| 11 |
|
| 12 |
var i Intern |
| 13 |
allocsPerRun := testing.AllocsPerRun(1000, func() { |
| 14 |
i = Intern{} |
| 15 |
err := easyjson.Unmarshal(data, &i) |
| 16 |
if err != nil { |
| 17 |
t.Error(err) |
| 18 |
} |
| 19 |
if i.Field != "string interning test" { |
| 20 |
t.Fatalf("wrong value: %q", i.Field) |
| 21 |
} |
| 22 |
}) |
| 23 |
if allocsPerRun > 1 { |
| 24 |
t.Fatalf("expected <= 1 allocs, got %f", allocsPerRun) |
| 25 |
} |
| 26 |
|
| 27 |
var n NoIntern |
| 28 |
allocsPerRun = testing.AllocsPerRun(1000, func() { |
| 29 |
n = NoIntern{} |
| 30 |
err := easyjson.Unmarshal(data, &n) |
| 31 |
if err != nil { |
| 32 |
t.Error(err) |
| 33 |
} |
| 34 |
if n.Field != "string interning test" { |
| 35 |
t.Fatalf("wrong value: %q", n.Field) |
| 36 |
} |
| 37 |
}) |
| 38 |
if allocsPerRun > 2 { |
| 39 |
t.Fatalf("expected <= 2 allocs, got %f", allocsPerRun) |
| 40 |
} |
| 41 |
} |