[varint] Add LEB128 decoding + tests

Also make tests in general less spammy: Don't print [PASS] for each successful test, only report failures and progress.
This commit is contained in:
Jeroen van Rijn
2022-03-08 15:40:00 +01:00
parent 29e660b16f
commit 6d7217f37a
13 changed files with 1138 additions and 1002 deletions
@@ -0,0 +1,92 @@
package test_core_json
import "core:encoding/json"
import "core:testing"
import "core:fmt"
import "core:os"
TEST_count := 0
TEST_fail := 0
when ODIN_TEST {
expect :: testing.expect
log :: testing.log
} else {
expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
TEST_count += 1
if !condition {
TEST_fail += 1
fmt.printf("[%v] %v\n", loc, message)
return
}
}
log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
fmt.printf("[%v] ", loc)
fmt.printf("log: %v\n", v)
}
}
main :: proc() {
t := testing.T{}
parse_json(&t)
marshal_json(&t)
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
if TEST_fail > 0 {
os.exit(1)
}
}
@test
parse_json :: proc(t: ^testing.T) {
json_data := `
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
`
_, err := json.parse(transmute([]u8)json_data)
expect(t, err == .None, "expected json error to be none")
}
@test
marshal_json :: proc(t: ^testing.T) {
My_Struct :: struct {
a: int,
b: int,
}
my_struct := My_Struct {
a = 2,
b = 5,
}
_, err := json.marshal(my_struct)
expect(t, err == .None, "expected json error to be none")
}