From 29f4ff923de0d0ce94323d161cf0ae15a1c0a582 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 9 Feb 2021 00:33:39 +0100 Subject: [PATCH] ext lib_test - add tests for struct field type char array (fixed) --- .../ext/lib_test/lib_test/gen/config.json | 12 +- examples/ext/lib_test/tests/test_lib_test.py | 138 +++++++++++++----- 2 files changed, 104 insertions(+), 46 deletions(-) diff --git a/examples/ext/lib_test/lib_test/gen/config.json b/examples/ext/lib_test/lib_test/gen/config.json index 598b28f..b756ed7 100644 --- a/examples/ext/lib_test/lib_test/gen/config.json +++ b/examples/ext/lib_test/lib_test/gen/config.json @@ -5,10 +5,6 @@ "variables": [ ], "functions": [ - "// func_TTAPSa_args_V", - "func_TACS_args_V" - ], - "//functions": [ "func_V_args_V", "func_Pi_args_V", "func_V_args_Pi", @@ -42,10 +38,10 @@ "func_ppTAPS_args_ppTAPS", "func_pppTAPS_args_pppTAPS", "func_TTAPSa_args_V", - "//func_TTAPSa_args_TTAPSa", - "//func_V_args_pTTAPSa", - "//func_ppTTAPSa_args_ppTTAPSa", - "//func_pppTTAPSa_args_pppTTAPSa", + "func_TTAPSa_args_TTAPSa", + "func_V_args_pTTAPSa", + "func_ppTTAPSa_args_ppTTAPSa", + "func_pppTTAPSa_args_pppTTAPSa", "func_TACS_args_V", "//func_TACS_args_TACS", "//func_V_args_pTACS", diff --git a/examples/ext/lib_test/tests/test_lib_test.py b/examples/ext/lib_test/tests/test_lib_test.py index 8373859..8fb4fab 100644 --- a/examples/ext/lib_test/tests/test_lib_test.py +++ b/examples/ext/lib_test/tests/test_lib_test.py @@ -172,8 +172,8 @@ def test_func_pppPc_args_pppPc_ppPc(): def test_func__PS_args_V(): (ret,) = func__PS_args_V() print(PS_to_string(ret)) - assert(ret.field__P_1 == 23); - assert(ret.field__P_2 == 23); + assert(ret.field__P_1 == 23) + assert(ret.field__P_2 == 23) def test_func__PS_args__PS(): ps = lib_test._PS() @@ -182,16 +182,16 @@ def test_func__PS_args__PS(): (ret,psout) = func__PS_args__PS(ps) print("ret:",PS_to_string(ret)) print("psout:",PS_to_string(psout)) - assert(ret.field__P_1 == 23); - assert(ret.field__P_2 == 23); - assert(psout.field__P_1 == 3); - assert(psout.field__P_2 == 3); + assert(ret.field__P_1 == 23) + assert(ret.field__P_2 == 23) + assert(psout.field__P_1 == 3) + assert(psout.field__P_2 == 3) def test_func_TTAPS_args_V(): (ret,) = func_TTAPS_args_V() print(PS_to_string(ret)) - assert(ret.field__P_1 == 23); - assert(ret.field__P_2 == 23); + assert(ret.field__P_1 == 23) + assert(ret.field__P_2 == 23) def test_func_TTAPS_args_TTAPS(): ps = PS() @@ -200,56 +200,127 @@ def test_func_TTAPS_args_TTAPS(): (ret,psout) = func_TTAPS_args_TTAPS(ps) print("ret:",PS_to_string(ret)) print("psout:",PS_to_string(psout)) - assert(ret.field__P_1 == 23); - assert(ret.field__P_2 == 23); - assert(psout.field__P_1 == 3); - assert(psout.field__P_2 == 3); + assert(ret.field__P_1 == 23) + assert(ret.field__P_2 == 23) + assert(psout.field__P_1 == 3) + assert(psout.field__P_2 == 3) # // By pointer def test_func_pTAPS_args_V(): (ret,) = func_pTAPS_args_V() print(PS_to_string(ret)) - assert(ret.field__P_1 == 23); - assert(ret.field__P_2 == 23); + assert(ret.field__P_1 == 23) + assert(ret.field__P_2 == 23) def test_func_ppTAPS_args_V(): (ret,) = func_ppTAPS_args_V() print(PS_to_string(ret)) - assert(ret.field__P_1 == 23); - assert(ret.field__P_2 == 23); + assert(ret.field__P_1 == 23) + assert(ret.field__P_2 == 23) def test_func_pppTAPS_args_V(): (ret,) = func_ppTAPS_args_V() print(PS_to_string(ret)) - assert(ret.field__P_1 == 23); - assert(ret.field__P_2 == 23); + assert(ret.field__P_1 == 23) + assert(ret.field__P_2 == 23) def test_func_V_args_pTAPS(): ps = PS() print(PS_to_string(ps)) (ret,psnew) = func_V_args_pTAPS(ps) print(PS_to_string(psnew)) - assert(ret == None); - assert(psnew.field__P_1 == 23); - assert(psnew.field__P_2 == 23); + assert(ret == None) + assert(psnew.field__P_1 == 23) + assert(psnew.field__P_2 == 23) def test_func_ppTAPS_args_ppTAPS(): ps = PS() print(PS_to_string(ps)) (ret,inout) = func_ppTAPS_args_ppTAPS(ps) print(PS_to_string(ret)) - assert(ret.field__P_1 == 23); - assert(inout.field__P_1 == 23); - assert(inout.field__P_2 == 23); + assert(ret.field__P_1 == 23) + assert(inout.field__P_1 == 23) + assert(inout.field__P_2 == 23) def test_func_pppTAPS_args_pppTAPS(): ps = PS() print(PS_to_string(ps)) (ret,inout) = func_pppTAPS_args_pppTAPS(ps) print(PS_to_string(ret)) - assert(ret.field__P_1 == 23); - assert(inout.field__P_1 == 23); - assert(inout.field__P_2 == 23); + assert(ret.field__P_1 == 23) + assert(inout.field__P_1 == 23) + assert(inout.field__P_2 == 23) + +# // PSa - Primitive Structs containing array +# // PSa contains a char[4] (which is an array of bytes, NOT a string) +# // By value +def test_func_TTAPSa_args_V(): + (ret,) = func_TTAPSa_args_V() + assert(ret.field__Pa_1 == b"pEp\x00") + +def test_func_TTAPSa_args_TTAPSa(): + psa = PSa() + psa.field__Pa_1 = b'pEp' + (ret,inout) = func_TTAPSa_args_TTAPSa(psa) + assert(ret.field__Pa_1 == b'pEp\x00') + assert(inout.field__Pa_1 == b'pEp\x00') + +# // By pointer +def test_func_V_args_pTTAPSa(): + psa = PSa() + (ret,inout) = func_V_args_pTTAPSa(psa) + assert(ret == None) + assert(inout.field__Pa_1 == b'pEp\x00') + +def test_func_ppTTAPSa_args_ppTTAPSa(): + psa = PSa() + (ret,inout) = func_ppTTAPSa_args_ppTTAPSa(psa) + assert(ret.field__Pa_1 == b'pEp\x00') + assert(inout.field__Pa_1 == b'pEp\x00') + +def test_func_pppTTAPSa_args_pppTTAPSa(): + psa = PSa() + (ret,inout) = func_pppTTAPSa_args_pppTTAPSa(psa) + assert(ret.field__Pa_1 == b'pEp\x00') + assert(inout.field__Pa_1 == b'pEp\x00') + +def test_struct_field_array_of_char_get_set(): + a = PSa() + a.field__Pa_1 = b'' + assert(a.field__Pa_1 == b'\x00\x00\x00\x00') + assert(len(a.field__Pa_1) == 4 ) + a.field__Pa_1 = b'pEp' + assert(a.field__Pa_1 == b'pEp\x00') + assert(len(a.field__Pa_1) == 4 ) + a.field__Pa_1 = b'pEpTooLong' + assert(a.field__Pa_1 == b'pEpT') + assert(len(a.field__Pa_1) == 4 ) + a.field__Pa_1 = b'p' + assert(a.field__Pa_1 == b'p\x00\x00\x00') + assert(len(a.field__Pa_1) == 4 ) + +# // CS - Complex Structs containing array +# // CS contains a PS[3] (which is an array of structs) +# // By value +# TACS func_TACS_args_V(); // MISSING +# +# TACS func_TACS_args_TACS(TACS arg1_TACS); // MISSING +# +# // By pointer +# void func_V_args_pTACS(TACS* arg1_pTACS); // MISSING +# +# TACS** func_ppTACS_args_ppTACS(TACS** arg1_ppTACS); // MISSING +# +# TACS*** func_pppTACS_args_pppTACS(TACS*** arg1_pppTACS); // MISSING + +def test_struct_field_array_of_structs_set_get(): + a = CS() + b = PS() + b.field__P_1 = 23 + b.field__P_2 = 23 + a.field_APS_1 = [b] + assert(a.field_APS_1[0].field__P_1 == 23) + assert(a.field_APS_1[0].field__P_2 == 23) def test_struct_field_array_of_structs_immutable_len(): a = CS() @@ -271,18 +342,9 @@ def test_struct_field_array_of_structs_accept_unityped_arr_only(): with pytest.raises(Exception): a.field_APS_1 = [b,"invalid_type"] -def test_struct_field_array_of_structs_of_structs_set_get(): - a = CS() - b = PS() - b.field__P_1 = 23 - b.field__P_2 = 23 - a.field_APS_1 = [b] - assert(a.field_APS_1[0].field__P_1 == 23) - assert(a.field_APS_1[0].field__P_2 == 23) - def PS_to_string(ps): ret = "{}\n".format(type(ps)) - ret += "field__P_1: {}\n".format(ps.field__P_1); - ret += "field__P_2: {}".format(ps.field__P_2); + ret += "field__P_1: {}\n".format(ps.field__P_1) + ret += "field__P_2: {}".format(ps.field__P_2) return ret