Something went wrong. Try again.
Ron's Dynamic EDitor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147package msgpack
import "core:slice"import "core:testing"
Object :: union { Nil, bool, i64, u64, f32, f64, string, Bin, Array, Map, Ext,}
Nil :: struct {}Bin :: distinct []byteArray :: distinct []ObjectMap :: distinct []Map_PairMap_Pair :: struct { key, value: Object,}Ext :: struct { type: i8, data: []byte,}
object_equals :: proc(a, b: Object) -> bool { switch x in a { case Nil: _, ok := b.(Nil) return ok case bool: y, ok := b.(bool) return ok && x == y case i64: y, ok := b.(i64) return ok && x == y case u64: y, ok := b.(u64) return ok && x == y case f32: y, ok := b.(f32) return ok && x == y case f64: y, ok := b.(f64) return ok && x == y case string: y, ok := b.(string) return ok && x == y case Bin: y, ok := b.(Bin) return ok && slice.equal(([]byte)(x), ([]byte)(y)) case Ext: y, ok := b.(Ext) return ok && x.type == y.type && slice.equal(x.data, y.data) case Array: y, ok := b.(Array) if !ok || len(x) != len(y) {return false} for e, i in x { if !object_equals(e, y[i]) {return false} } return true case Map: y, ok := b.(Map) if !ok || len(x) != len(y) {return false} // Maps are unordered, so comparison is a bit more involved here for pair in x { in_x, in_y := 0, 0 for other in x { if object_equals(pair.key, other.key) && object_equals(pair.value, other.value) { in_x += 1 } } for other in y { if object_equals(pair.key, other.key) && object_equals(pair.value, other.value) { in_y += 1 } } if in_x != in_y {return false} } return true } return a == nil && b == nil}
@(test)test_object_equals :: proc(t: ^testing.T) { Same :: struct { a, b: Object, } same := []Same { {Nil{}, Nil{}}, {true, true}, {i64(-1), i64(-1)}, {u64(1), u64(1)}, {f32(1.5), f32(1.5)}, {f64(-1.5), f64(-1.5)}, {"hello", "hello"}, {Bin{}, Bin{}}, {Bin{0x01, 0x02}, Bin{0x01, 0x02}}, {Ext{type = 7, data = {0x01}}, Ext{type = 7, data = {0x01}}}, {Array{}, Array{}}, {Array{i64(1), "a", Array{Nil{}}}, Array{i64(1), "a", Array{Nil{}}}}, {Map{}, Map{}}, {Map{{"a", i64(1)}, {"b", i64(2)}}, Map{{"b", i64(2)}, {"a", i64(1)}}}, {Map{{"a", i64(1)}, {"a", i64(1)}}, Map{{"a", i64(1)}, {"a", i64(1)}}}, } for c in same { testing.expectf(t, object_equals(c.a, c.b), "%v should equal %v", c.a, c.b) testing.expectf(t, object_equals(c.b, c.a), "%v should equal %v", c.b, c.a) }
Different :: struct { a, b: Object, } different := []Different { {Nil{}, false}, {true, false}, {i64(1), u64(1)}, {i64(1), i64(2)}, {u64(1), u64(2)}, {f32(1.5), f64(1.5)}, {f32(1.5), f32(2.5)}, {f64(1.5), f64(2.5)}, {"a", "b"}, {Bin{0x01}, Bin{0x01, 0x02}}, {Bin{0x01}, Bin{0x02}}, {Ext{type = 1, data = {0x01}}, Ext{type = 2, data = {0x01}}}, {Ext{type = 1, data = {0x01}}, Ext{type = 1, data = {0x02}}}, {Array{i64(1)}, Array{i64(1), i64(2)}}, {Array{i64(1)}, Array{"1"}}, {Map{{"a", i64(1)}}, Map{{"a", i64(2)}}}, {Map{{"a", i64(1)}}, Map{{"b", i64(1)}}}, {Map{{"a", i64(1)}}, Map{{"a", i64(1)}, {"b", i64(1)}}}, {Map{{"a", i64(1)}, {"a", i64(1)}}, Map{{"a", i64(1)}, {"a", i64(2)}}}, } for c in different { testing.expectf(t, !object_equals(c.a, c.b), "%v should not equal %v", c.a, c.b) testing.expectf(t, !object_equals(c.b, c.a), "%v should not equal %v", c.b, c.a) }}