aboutsummaryrefslogtreecommitdiff
path: root/tests/core/encoding
diff options
context:
space:
mode:
authorVladPavliuk <pavliuk.vlad@gmail.com>2024-07-13 00:07:48 +0300
committerVladPavliuk <pavliuk.vlad@gmail.com>2024-07-13 00:07:48 +0300
commitdbdad0476d1b9dd72eddd6b8584beca855585b13 (patch)
tree39ff20c4ab923e8f261e3e0f4bcfee25e5dc700f /tests/core/encoding
parent1770e3a9a97620125f6333db7678dd4ec34fa329 (diff)
Allow to `marshal` and `unmarshal` maps with int keys
Diffstat (limited to 'tests/core/encoding')
-rw-r--r--tests/core/encoding/json/test_core_json.odin40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/core/encoding/json/test_core_json.odin b/tests/core/encoding/json/test_core_json.odin
index 92c050952..dfc655af0 100644
--- a/tests/core/encoding/json/test_core_json.odin
+++ b/tests/core/encoding/json/test_core_json.odin
@@ -3,6 +3,10 @@ package test_core_json
import "core:encoding/json"
import "core:testing"
import "core:mem/virtual"
+import "core:fmt"
+import "base:runtime"
+import "core:log"
+import "core:strings"
@test
parse_json :: proc(t: ^testing.T) {
@@ -368,4 +372,40 @@ utf8_string_of_multibyte_characters :: proc(t: ^testing.T) {
val, err := json.parse_string(`"🐛✅"`)
defer json.destroy_value(val)
testing.expectf(t, err == nil, "Expected `json.parse` to return nil, got %v", err)
+}
+
+@test
+map_with_integer_keys :: proc(t: ^testing.T) {
+ my_map := make(map[i32]string)
+ defer delete_map(my_map)
+
+ my_map[-1] = "a"
+ my_map[0] = "b"
+ my_map[42] = "c"
+ my_map[99999999] = "d"
+
+ marshaled_data, marshal_err := json.marshal(my_map)
+ defer delete(marshaled_data)
+
+ testing.expectf(t, marshal_err == nil, "Expected `json.marshal` to return nil error, got %v", marshal_err)
+
+ my_map2 := make(map[i32]string)
+ defer delete_map(my_map2)
+
+ unmarshal_err := json.unmarshal(marshaled_data, &my_map2)
+ defer for key, item in my_map2 {
+ runtime.delete_string(item)
+ }
+ testing.expectf(t, unmarshal_err == nil, "Expected `json.unmarshal` to return nil, got %v", unmarshal_err)
+
+ testing.expectf(t, len(my_map) == len(my_map2), "Expected %v map items to have been unmarshaled, got %v", len(my_map), len(my_map2))
+
+ for key, item in my_map {
+ testing.expectf(t, key in my_map2, "Expected key %v to be present in unmarshaled map", key)
+
+ value_from_map2, ok := my_map2[key]
+ if ok {
+ testing.expectf(t, runtime.string_eq(item, my_map2[key]), "Expected value %s to be present in unmarshaled map", key)
+ }
+ }
} \ No newline at end of file