Debian Codegraph @

bytes_unsafe_test.go

raw symbols SCIP
1 //go:build !appengine && !appenginevm
2 // +build !appengine,!appenginevm
3
4 package jsonparserscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/
5
6 import (
7 "reflectscip-go gomod github.com/golang/go/src go1.13 reflect/"
8 "stringsscip-go gomod github.com/golang/go/src go1.13 strings/"
9 "testingscip-go gomod github.com/golang/go/src go1.13 testing/"
10 "unsafescip-go gomod github.com/golang/go/src go1.13 unsafe/"
11 )
12
13 var (
14 // short string/[]byte sequences, as the difference between these
15 // three methods is a constant overhead
16 benchmarkStringscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkString. = "0123456789x"
17 benchmarkBytesscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkBytes. = []byte("0123456789y")
18 )
19
20 // Test helper for SYS-REQ-001 and SYS-REQ-008.
21 func bytesEqualStrSafescip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/bytesEqualStrSafe().(abytes []byte, bstr string) bool {
22 return bstr == string(abytes)
23 }
24
25 // Test helper for SYS-REQ-001 and SYS-REQ-008.
26 func bytesEqualStrUnsafeSlowerscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/bytesEqualStrUnsafeSlower().(abytes *[]byte, bstr string) bool {
27 aslicehdr := (*reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.SliceHeaderscip-go gomod github.com/golang/go/src go1.13 reflect/SliceHeader#)(unsafescip-go gomod github.com/golang/go/src go1.13 unsafe/.Pointer(abytes))
28 astrhdr := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.StringHeaderscip-go gomod github.com/golang/go/src go1.13 reflect/StringHeader#{Datascip-go gomod github.com/golang/go/src go1.13 reflect/StringHeader#Data.: aslicehdr.Datascip-go gomod github.com/golang/go/src go1.13 reflect/SliceHeader#Data., Lenscip-go gomod github.com/golang/go/src go1.13 reflect/StringHeader#Len.: aslicehdr.Lenscip-go gomod github.com/golang/go/src go1.13 reflect/SliceHeader#Len.}
29 return *(*string)(unsafescip-go gomod github.com/golang/go/src go1.13 unsafe/.Pointer(&astrhdr)) == bstr
30 }
31
32 // Verifies: SYS-REQ-001
33 // MCDC SYS-REQ-001: N/A
34 func TestEqualscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/TestEqual().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) {
35 if !equalStrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/equalStr().(&[]byte{}, "") {
36 t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(`equalStr("", ""): expected true, obtained false`)
37 return
38 }
39
40 longstr := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Repeatscip-go gomod github.com/golang/go/src go1.13 strings/Repeat().("a", 1000)
41 for i := 0; i < len(longstr); i++ {
42 s1, s2 := longstr[:i]+"1", longstr[:i]+"2"
43 b1 := []byte(s1)
44
45 if !equalStrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/equalStr().(&b1, s1) {
46 t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(`equalStr("a"*%d + "1", "a"*%d + "1"): expected true, obtained false`, i, i)
47 break
48 }
49 if equalStrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/equalStr().(&b1, s2) {
50 t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().(`equalStr("a"*%d + "1", "a"*%d + "2"): expected false, obtained true`, i, i)
51 break
52 }
53 }
54 }
55
56 // Verifies: SYS-REQ-001
57 // MCDC SYS-REQ-001: N/A
58 func BenchmarkEqualStrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/BenchmarkEqualStr().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) {
59 for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ {
60 equalStrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/equalStr().(&benchmarkBytesscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkBytes., benchmarkStringscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkString.)
61 }
62 }
63
64 // Alternative implementation without using unsafe
65 // Verifies: SYS-REQ-001
66 // MCDC SYS-REQ-001: N/A
67 func BenchmarkBytesEqualStrSafescip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/BenchmarkBytesEqualStrSafe().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) {
68 for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ {
69 bytesEqualStrSafescip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/bytesEqualStrSafe().(benchmarkBytesscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkBytes., benchmarkStringscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkString.)
70 }
71 }
72
73 // Alternative implementation using unsafe, but that is slower than the current implementation
74 // Verifies: SYS-REQ-001
75 // MCDC SYS-REQ-001: N/A
76 func BenchmarkBytesEqualStrUnsafeSlowerscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/BenchmarkBytesEqualStrUnsafeSlower().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) {
77 for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ {
78 bytesEqualStrUnsafeSlowerscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/bytesEqualStrUnsafeSlower().(&benchmarkBytesscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkBytes., benchmarkStringscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/benchmarkString.)
79 }
80 }