From 3a656e7b049f365e302d30b07566a266d282d6db Mon Sep 17 00:00:00 2001 From: eine Date: Mon, 13 Jan 2020 01:41:52 +0100 Subject: [PATCH 1/3] add jsonGetBooleanArray and jsonGetRealArray --- data/Boards0.json | 2 +- examples/Boards_VUnit.vhdl | 27 ++++++++++------- src/JSON.ctx.vhdl | 2 ++ src/JSON.pkg.vhdl | 60 ++++++++++++++++++++++++++++++++++---- 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/data/Boards0.json b/data/Boards0.json index 3aa5dd15..c5c3db6b 100644 --- a/data/Boards0.json +++ b/data/Boards0.json @@ -1 +1 @@ - ["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,"world"] + ["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,[2.5, 3.33, 5.25],"world"] diff --git a/examples/Boards_VUnit.vhdl b/examples/Boards_VUnit.vhdl index 4fd8de96..a49dd8c0 100644 --- a/examples/Boards_VUnit.vhdl +++ b/examples/Boards_VUnit.vhdl @@ -14,28 +14,33 @@ end entity; architecture tb of tb_boards is procedure test_board0(JSONContent : T_JSON) is - constant img_arr : integer_vector := jsonGetIntegerArray(JSONContent, "2"); - constant ref_arr : integer_vector := (9,8); + constant int_arr : integer_vector := jsonGetIntegerArray(JSONContent, "2"); + constant bool_arr : boolean_vector := jsonGetBooleanArray(JSONContent, "1"); + constant real_arr : real_vector := jsonGetRealArray(JSONContent, "5"); + constant int_ref_arr : integer_vector := (9,8); + constant bool_ref_arr : boolean_vector := (true,false); + constant real_ref_arr : real_vector := (2.5, 3.33, 5.25); begin assert jsonGetString(JSONContent, "0") = "test" severity failure; - assert jsonGetBoolean(JSONContent, "1/0") severity failure; assert not jsonGetBoolean(JSONContent, "1/1") severity failure; assert positive'value(jsonGetString(JSONContent, "1/2")) = 18 severity failure; assert jsonIsNull(JSONContent, "1/3") severity failure; assert jsonGetString(JSONContent, "1/4") = "hello" severity failure; - assert jsonGetString(JSONContent, "2/0") = "9" severity failure; assert jsonGetString(JSONContent, "2/1") = "8" severity failure; - for i in 0 to img_arr'length-1 loop - assert img_arr(i) = ref_arr(i) severity failure; - end loop; - assert real'value(jsonGetString(JSONContent, "3")) = 3324.34 severity failure; - assert natural'value(jsonGetString(JSONContent, "4")) = 832432 severity failure; - - assert jsonGetString(JSONContent, "5") = "world" severity failure; + assert jsonGetString(JSONContent, "6") = "world" severity failure; + for i in 0 to int_ref_arr'length-1 loop + check_equal(int_arr(i), int_ref_arr(i)); + end loop; + for i in 0 to bool_ref_arr'length-1 loop + check_equal(bool_arr(i), bool_ref_arr(i)); + end loop; + for i in 0 to real_ref_arr'length-1 loop + check_equal(real_arr(i), real_ref_arr(i), max_diff => real_ref_arr(i)*2.0**(-52)); + end loop; end procedure; procedure test_board1(JSONContent : T_JSON) is diff --git a/src/JSON.ctx.vhdl b/src/JSON.ctx.vhdl index ca401e9a..3abe418c 100644 --- a/src/JSON.ctx.vhdl +++ b/src/JSON.ctx.vhdl @@ -44,7 +44,9 @@ context json_ctx is use JSON.json.jsonGetContent; use JSON.json.jsonGetBoolean; use JSON.json.jsonGetString; + use JSON.json.jsonGetBooleanArray; use JSON.json.jsonGetIntegerArray; + use JSON.json.jsonGetRealArray; use JSON.json.jsonIsBoolean; use JSON.json.jsonIsNull; use JSON.json.jsonIsString; diff --git a/src/JSON.pkg.vhdl b/src/JSON.pkg.vhdl index 11183c6c..e82e0a23 100644 --- a/src/JSON.pkg.vhdl +++ b/src/JSON.pkg.vhdl @@ -105,10 +105,15 @@ package JSON is function jsonGetBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonGetString(JSONContext : T_JSON; Path : STRING) return STRING; + + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string) return boolean_vector; + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string; Len : positive) return boolean_vector; function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector; function jsonGetIntegerArray(JSONContext : T_JSON; Path : string; Len : positive) return integer_vector; --- function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector; + function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector; + function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector; + function jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) return natural; function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonIsNull(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonIsString(JSONContext : T_JSON; Path : STRING) return BOOLEAN; @@ -1639,14 +1644,31 @@ package body JSON is return (Element.ElementType = ELEM_TRUE); end function; - -- function to get a integer_vector from the compressed content extracted from a JSON input - function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector is - variable len: natural := 0; + -- function to get a boolean_vector from the compressed content extracted from a JSON input + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string) return boolean_vector is + variable len: natural:=0; begin - while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop + while jsonIsBoolean(JSONContext, Path & "/" & to_string(len)) loop len := len+1; end loop; - return jsonGetIntegerArray(JSONContext, Path, len); + return jsonGetBooleanArray(JSONContext, Path, len); + end function; + + -- function to get a boolean_vector of a fixed length from the compressed content extracted from a JSON input + function jsonGetBooleanArray(JSONContext : T_JSON; Path : string; Len : positive) return boolean_vector is + variable return_value : boolean_vector(Len-1 downto 0); + begin + for i in 0 to Len-1 loop + return_value(i) := boolean'value(jsonGetString(JSONContext, Path & "/" & to_string(i))); + end loop; + return return_value; + end function; + + -- function to get a integer_vector from the compressed content extracted from a JSON input + function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector is + variable len: natural:=0; + begin + return jsonGetIntegerArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path)); end function; -- function to get a integer_vector of a fixed length from the compressed content extracted from a JSON input @@ -1659,6 +1681,32 @@ package body JSON is return return_value; end function; + -- function to get a real_vector from the compressed content extracted from a JSON input + function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector is + variable len: natural:=0; + begin + return jsonGetRealArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path)); + end function; + + -- function to get a real_vector of a fixed length from the compressed content extracted from a JSON input + function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector is + variable return_value : real_vector(Len-1 downto 0); + begin + for i in 0 to Len-1 loop + return_value(i) := real'value(jsonGetString(JSONContext, Path & "/" & to_string(i))); + end loop; + return return_value; + end function; + + function jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) return natural is + variable len: natural:=0; + begin + while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop + len := len+1; + end loop; + return len; + end function; + function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN is constant ElementIndex : T_UINT16 := jsonGetElementIndex(JSONContext, Path); constant Element : T_JSON_INDEX_ELEMENT := JSONContext.Index(ElementIndex); From 08a118d2c94162981f390f57242f67160a8c04da Mon Sep 17 00:00:00 2001 From: eine Date: Mon, 13 Jan 2020 01:42:00 +0100 Subject: [PATCH 2/3] add jsonGetArrayLength --- src/JSON.ctx.vhdl | 1 + src/JSON.pkg.vhdl | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/JSON.ctx.vhdl b/src/JSON.ctx.vhdl index 3abe418c..90078729 100644 --- a/src/JSON.ctx.vhdl +++ b/src/JSON.ctx.vhdl @@ -47,6 +47,7 @@ context json_ctx is use JSON.json.jsonGetBooleanArray; use JSON.json.jsonGetIntegerArray; use JSON.json.jsonGetRealArray; + use JSON.json.jsonGetArrayLength; use JSON.json.jsonIsBoolean; use JSON.json.jsonIsNull; use JSON.json.jsonIsString; diff --git a/src/JSON.pkg.vhdl b/src/JSON.pkg.vhdl index e82e0a23..d01d2385 100644 --- a/src/JSON.pkg.vhdl +++ b/src/JSON.pkg.vhdl @@ -113,7 +113,7 @@ package JSON is function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector; function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector; - function jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) return natural; + function jsonGetArrayLength(JSONContext : T_JSON; Path : string; IsType: character) return natural; function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonIsNull(JSONContext : T_JSON; Path : STRING) return BOOLEAN; function jsonIsString(JSONContext : T_JSON; Path : STRING) return BOOLEAN; @@ -1646,13 +1646,9 @@ package body JSON is -- function to get a boolean_vector from the compressed content extracted from a JSON input function jsonGetBooleanArray(JSONContext : T_JSON; Path : string) return boolean_vector is - variable len: natural:=0; begin - while jsonIsBoolean(JSONContext, Path & "/" & to_string(len)) loop - len := len+1; - end loop; - return jsonGetBooleanArray(JSONContext, Path, len); - end function; + return jsonGetBooleanArray(JSONContext, Path, jsonGetArrayLength(JSONContext, Path, 'b')); + end; -- function to get a boolean_vector of a fixed length from the compressed content extracted from a JSON input function jsonGetBooleanArray(JSONContext : T_JSON; Path : string; Len : positive) return boolean_vector is @@ -1662,14 +1658,13 @@ package body JSON is return_value(i) := boolean'value(jsonGetString(JSONContext, Path & "/" & to_string(i))); end loop; return return_value; - end function; + end; -- function to get a integer_vector from the compressed content extracted from a JSON input function jsonGetIntegerArray(JSONContext : T_JSON; Path : string) return integer_vector is - variable len: natural:=0; begin - return jsonGetIntegerArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path)); - end function; + return jsonGetIntegerArray(JSONContext, Path, jsonGetArrayLength(JSONContext, Path, 'i')); + end; -- function to get a integer_vector of a fixed length from the compressed content extracted from a JSON input function jsonGetIntegerArray(JSONContext : T_JSON; Path : string; Len : positive) return integer_vector is @@ -1679,14 +1674,13 @@ package body JSON is return_value(i) := to_natural_dec(jsonGetString(JSONContext, Path & "/" & to_string(i))); end loop; return return_value; - end function; + end; -- function to get a real_vector from the compressed content extracted from a JSON input function jsonGetRealArray(JSONContext : T_JSON; Path : string) return real_vector is - variable len: natural:=0; begin - return jsonGetRealArray(JSONContext, Path, jsonGetNumberArrayLength(JSONContext, Path)); - end function; + return jsonGetRealArray(JSONContext, Path, jsonGetArrayLength(JSONContext, Path, 'r')); + end; -- function to get a real_vector of a fixed length from the compressed content extracted from a JSON input function jsonGetRealArray(JSONContext : T_JSON; Path : string; Len : positive) return real_vector is @@ -1696,16 +1690,21 @@ package body JSON is return_value(i) := real'value(jsonGetString(JSONContext, Path & "/" & to_string(i))); end loop; return return_value; - end function; + end; - function jsonGetNumberArrayLength(JSONContext : T_JSON; Path : string) return natural is + function jsonGetArrayLength(JSONContext : T_JSON; Path : string; IsType: character) return natural is variable len: natural:=0; + variable val: boolean:=true; begin - while jsonIsNumber(JSONContext, Path & "/" & to_string(len)) loop - len := len+1; + while(val) loop + case IsType is + when 'b' => val := jsonIsBoolean(JSONContext, Path & "/" & to_string(len)); + when others => val := jsonIsNumber(JSONContext, Path & "/" & to_string(len)); + end case; + len := len+1; end loop; - return len; - end function; + return len-1; + end; function jsonIsBoolean(JSONContext : T_JSON; Path : STRING) return BOOLEAN is constant ElementIndex : T_UINT16 := jsonGetElementIndex(JSONContext, Path); From ea1e648b0ff2fdd1d4cecf9205e8d862dc089722 Mon Sep 17 00:00:00 2001 From: eine Date: Mon, 13 Jan 2020 06:13:01 +0100 Subject: [PATCH 3/3] keep the size of Boards0.json reduced --- data/Boards0.json | 2 +- data/Boards0b.json | 1 + tests/VUnit/run.py | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 data/Boards0b.json diff --git a/data/Boards0.json b/data/Boards0.json index c5c3db6b..3aa5dd15 100644 --- a/data/Boards0.json +++ b/data/Boards0.json @@ -1 +1 @@ - ["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,[2.5, 3.33, 5.25],"world"] + ["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,"world"] diff --git a/data/Boards0b.json b/data/Boards0b.json new file mode 100644 index 00000000..c5c3db6b --- /dev/null +++ b/data/Boards0b.json @@ -0,0 +1 @@ + ["test",[true,false,18,null,"hello"],[9,8],3324.34,832432,[2.5, 3.33, 5.25],"world"] diff --git a/tests/VUnit/run.py b/tests/VUnit/run.py index bdcf5dc2..b673a804 100644 --- a/tests/VUnit/run.py +++ b/tests/VUnit/run.py @@ -33,7 +33,7 @@ def b16enc(s): tb.get_tests("stringified*")[0].set_generic("tb_cfg", gen_str) tb.get_tests("b16encoded stringified*")[0].set_generic("tb_cfg", b16enc(gen_str)) -tb.get_tests("JSON file*")[0].set_generic("tb_cfg", join(root, 'data', 'Boards0.json')) -tb.get_tests("b16encoded JSON file*")[0].set_generic("tb_cfg", b16enc(join(root, 'data', 'Boards0.json'))) +tb.get_tests("JSON file*")[0].set_generic("tb_cfg", join(root, 'data', 'Boards0b.json')) +tb.get_tests("b16encoded JSON file*")[0].set_generic("tb_cfg", b16enc(join(root, 'data', 'Boards0b.json'))) vu.main()