From 2e3224a138832b786c4c2905b93208d27b37d358 Mon Sep 17 00:00:00 2001 From: Rickard Andersson Date: Mon, 2 Oct 2023 15:17:06 +0300 Subject: [PATCH] testing: add test for `Out_Of_Memory` return --- tests/core/encoding/json/test_core_json.odin | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/core/encoding/json/test_core_json.odin b/tests/core/encoding/json/test_core_json.odin index 23361d694..813d11b2c 100644 --- a/tests/core/encoding/json/test_core_json.odin +++ b/tests/core/encoding/json/test_core_json.odin @@ -4,6 +4,7 @@ import "core:encoding/json" import "core:testing" import "core:fmt" import "core:os" +import "core:mem/virtual" TEST_count := 0 TEST_fail := 0 @@ -77,6 +78,49 @@ parse_json :: proc(t: ^testing.T) { expect(t, err == nil, msg) } +@test +out_of_memory_in_parse_json :: proc(t: ^testing.T) { + arena: virtual.Arena + arena_buffer: [256]byte + arena_init_error := virtual.arena_init_buffer(&arena, arena_buffer[:]) + testing.expect(t, arena_init_error == nil, fmt.tprintf("Expected arena initialization to not return error, got: %v\n", arena_init_error)) + + context.allocator = virtual.arena_allocator(&arena) + + 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) + + expected_error := json.Error.Out_Of_Memory + msg := fmt.tprintf("Expected `json.parse` to fail with %v, got %v", expected_error, err) + expect(t, err == json.Error.Out_Of_Memory, msg) +} + @test marshal_json :: proc(t: ^testing.T) {