mirror of https://github.com/tidwall/gjson.git
reduced some complexity
This commit is contained in:
parent
766ad13f76
commit
92a0497357
|
@ -174,8 +174,9 @@ func TestBasic(t *testing.T) {
|
||||||
if mtok.String() != `["Brett","Elliotte"]` {
|
if mtok.String() != `["Brett","Elliotte"]` {
|
||||||
t.Fatalf("expected %v, got %v", `["Brett","Elliotte"]`, mtok.String())
|
t.Fatalf("expected %v, got %v", `["Brett","Elliotte"]`, mtok.String())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
mtok = get(basicJSON, `loggy.programmers`)
|
func TestBasic1(t *testing.T) {
|
||||||
|
mtok := get(basicJSON, `loggy.programmers`)
|
||||||
var count int
|
var count int
|
||||||
mtok.ForEach(func(key, value Result) bool {
|
mtok.ForEach(func(key, value Result) bool {
|
||||||
if key.Exists() {
|
if key.Exists() {
|
||||||
|
@ -211,7 +212,9 @@ func TestBasic(t *testing.T) {
|
||||||
if count != 3 {
|
if count != 3 {
|
||||||
t.Fatalf("expected %v, got %v", 3, count)
|
t.Fatalf("expected %v, got %v", 3, count)
|
||||||
}
|
}
|
||||||
mtok = get(basicJSON, `loggy.programmers.#[age=101].firstName`)
|
}
|
||||||
|
func TestBasic2(t *testing.T) {
|
||||||
|
mtok := get(basicJSON, `loggy.programmers.#[age=101].firstName`)
|
||||||
if mtok.String() != "1002.3" {
|
if mtok.String() != "1002.3" {
|
||||||
t.Fatalf("expected %v, got %v", "1002.3", mtok.String())
|
t.Fatalf("expected %v, got %v", "1002.3", mtok.String())
|
||||||
}
|
}
|
||||||
|
@ -238,7 +241,9 @@ func TestBasic(t *testing.T) {
|
||||||
if programmers.Array()[1].Map()["firstName"].Str != "Jason" {
|
if programmers.Array()[1].Map()["firstName"].Str != "Jason" {
|
||||||
t.Fatalf("expected %v, got %v", "Jason", mtok.Map()["programmers"].Array()[1].Map()["firstName"].Str)
|
t.Fatalf("expected %v, got %v", "Jason", mtok.Map()["programmers"].Array()[1].Map()["firstName"].Str)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
func TestBasic3(t *testing.T) {
|
||||||
|
var mtok Result
|
||||||
if Parse(basicJSON).Get("loggy.programmers").Get("1").Get("firstName").Str != "Jason" {
|
if Parse(basicJSON).Get("loggy.programmers").Get("1").Get("firstName").Str != "Jason" {
|
||||||
t.Fatalf("expected %v, got %v", "Jason", Parse(basicJSON).Get("loggy.programmers").Get("1").Get("firstName").Str)
|
t.Fatalf("expected %v, got %v", "Jason", Parse(basicJSON).Get("loggy.programmers").Get("1").Get("firstName").Str)
|
||||||
}
|
}
|
||||||
|
@ -274,7 +279,8 @@ func TestBasic(t *testing.T) {
|
||||||
if len(mtok.Array()) != 0 {
|
if len(mtok.Array()) != 0 {
|
||||||
t.Fatalf("expected 0, got %v", len(mtok.Array()))
|
t.Fatalf("expected 0, got %v", len(mtok.Array()))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
func TestBasic4(t *testing.T) {
|
||||||
if get(basicJSON, "items.3.tags.#").Num != 3 {
|
if get(basicJSON, "items.3.tags.#").Num != 3 {
|
||||||
t.Fatalf("expected 3, got %v", get(basicJSON, "items.3.tags.#").Num)
|
t.Fatalf("expected 3, got %v", get(basicJSON, "items.3.tags.#").Num)
|
||||||
}
|
}
|
||||||
|
@ -290,7 +296,7 @@ func TestBasic(t *testing.T) {
|
||||||
if !get(basicJSON, "name.last").Exists() {
|
if !get(basicJSON, "name.last").Exists() {
|
||||||
t.Fatal("expected true, got false")
|
t.Fatal("expected true, got false")
|
||||||
}
|
}
|
||||||
token = get(basicJSON, "name.here")
|
token := get(basicJSON, "name.here")
|
||||||
if token.String() != "B\\\"R" {
|
if token.String() != "B\\\"R" {
|
||||||
t.Fatal("expecting 'B\\\"R'", "got", token.String())
|
t.Fatal("expecting 'B\\\"R'", "got", token.String())
|
||||||
}
|
}
|
||||||
|
@ -315,7 +321,9 @@ func TestBasic(t *testing.T) {
|
||||||
if token.Value() != nil {
|
if token.Value() != nil {
|
||||||
t.Fatal("should be nil")
|
t.Fatal("should be nil")
|
||||||
}
|
}
|
||||||
token = get(basicJSON, "age")
|
}
|
||||||
|
func TestBasic5(t *testing.T) {
|
||||||
|
token := get(basicJSON, "age")
|
||||||
if token.String() != "100" {
|
if token.String() != "100" {
|
||||||
t.Fatal("expecting '100'", "got", token.String())
|
t.Fatal("expecting '100'", "got", token.String())
|
||||||
}
|
}
|
||||||
|
@ -922,34 +930,22 @@ func BenchmarkFFJSONLexer(t *testing.B) {
|
||||||
t.N *= len(benchPaths) // because we are running against 3 paths
|
t.N *= len(benchPaths) // because we are running against 3 paths
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkEasyJSONLexer(t *testing.B) {
|
func skipCC(l *jlexer.Lexer, n int) {
|
||||||
skipCC := func(l *jlexer.Lexer, n int) {
|
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
l.Skip()
|
l.Skip()
|
||||||
l.WantColon()
|
l.WantColon()
|
||||||
l.Skip()
|
l.Skip()
|
||||||
l.WantComma()
|
l.WantComma()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
skipGroup := func(l *jlexer.Lexer, n int) {
|
func skipGroup(l *jlexer.Lexer, n int) {
|
||||||
l.WantColon()
|
l.WantColon()
|
||||||
l.Delim('{')
|
l.Delim('{')
|
||||||
skipCC(l, n)
|
skipCC(l, n)
|
||||||
l.Delim('}')
|
l.Delim('}')
|
||||||
l.WantComma()
|
l.WantComma()
|
||||||
}
|
}
|
||||||
t.ReportAllocs()
|
func easyJSONWindowName(t *testing.B, l *jlexer.Lexer) {
|
||||||
t.ResetTimer()
|
|
||||||
for i := 0; i < t.N; i++ {
|
|
||||||
for j := 0; j < len(benchPaths); j++ {
|
|
||||||
l := &jlexer.Lexer{Data: []byte(exampleJSON)}
|
|
||||||
l.Delim('{')
|
|
||||||
if l.String() == "widget" {
|
|
||||||
l.WantColon()
|
|
||||||
l.Delim('{')
|
|
||||||
switch benchPaths[j] {
|
|
||||||
case "widget.window.name":
|
|
||||||
skipCC(l, 1)
|
|
||||||
if l.String() == "window" {
|
if l.String() == "window" {
|
||||||
l.WantColon()
|
l.WantColon()
|
||||||
l.Delim('{')
|
l.Delim('{')
|
||||||
|
@ -961,11 +957,8 @@ func BenchmarkEasyJSONLexer(t *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "widget.image.hOffset":
|
}
|
||||||
skipCC(l, 1)
|
func easyJSONImageHOffset(t *testing.B, l *jlexer.Lexer) {
|
||||||
if l.String() == "window" {
|
|
||||||
skipGroup(l, 4)
|
|
||||||
}
|
|
||||||
if l.String() == "image" {
|
if l.String() == "image" {
|
||||||
l.WantColon()
|
l.WantColon()
|
||||||
l.Delim('{')
|
l.Delim('{')
|
||||||
|
@ -977,14 +970,8 @@ func BenchmarkEasyJSONLexer(t *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "widget.text.onMouseUp":
|
}
|
||||||
skipCC(l, 1)
|
func easyJSONTextOnMouseUp(t *testing.B, l *jlexer.Lexer) {
|
||||||
if l.String() == "window" {
|
|
||||||
skipGroup(l, 4)
|
|
||||||
}
|
|
||||||
if l.String() == "image" {
|
|
||||||
skipGroup(l, 4)
|
|
||||||
}
|
|
||||||
if l.String() == "text" {
|
if l.String() == "text" {
|
||||||
l.WantColon()
|
l.WantColon()
|
||||||
l.Delim('{')
|
l.Delim('{')
|
||||||
|
@ -996,7 +983,40 @@ func BenchmarkEasyJSONLexer(t *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
func easyJSONWidget(t *testing.B, l *jlexer.Lexer, j int) {
|
||||||
|
l.WantColon()
|
||||||
|
l.Delim('{')
|
||||||
|
switch benchPaths[j] {
|
||||||
|
case "widget.window.name":
|
||||||
|
skipCC(l, 1)
|
||||||
|
easyJSONWindowName(t, l)
|
||||||
|
case "widget.image.hOffset":
|
||||||
|
skipCC(l, 1)
|
||||||
|
if l.String() == "window" {
|
||||||
|
skipGroup(l, 4)
|
||||||
}
|
}
|
||||||
|
easyJSONImageHOffset(t, l)
|
||||||
|
case "widget.text.onMouseUp":
|
||||||
|
skipCC(l, 1)
|
||||||
|
if l.String() == "window" {
|
||||||
|
skipGroup(l, 4)
|
||||||
|
}
|
||||||
|
if l.String() == "image" {
|
||||||
|
skipGroup(l, 4)
|
||||||
|
}
|
||||||
|
easyJSONTextOnMouseUp(t, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func BenchmarkEasyJSONLexer(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
for j := 0; j < len(benchPaths); j++ {
|
||||||
|
l := &jlexer.Lexer{Data: []byte(exampleJSON)}
|
||||||
|
l.Delim('{')
|
||||||
|
if l.String() == "widget" {
|
||||||
|
easyJSONWidget(t, l, j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue