From 93e3cab8c926e21f1cdbb4eff7b863264b021441 Mon Sep 17 00:00:00 2001 From: David René Date: Wed, 17 Jan 2007 21:33:49 +0000 Subject: [PATCH] unit test first release --- anubis_dev/library/network/tools.anubis | 21 +++++++++++++++++++++ anubis_dev/library/system/convert.anubis | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ anubis_dev/library/system/muscle.anubis | 55 ++++++++++++++++++++++++++++++++++++++++++------------- anubis_dev/library/system/types.anubis | 31 +++++++++++++++++++++++++++++++ anubis_dev/library/test/all_unit_test.anubis | 19 +++++++++++++++++++ anubis_dev/library/test/system/convert.unit_test.anubis | 30 ++++++++++++++++++++++++++++++ anubis_dev/library/tools/unit_test.anubis | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 260 insertions(+), 13 deletions(-) create mode 100644 anubis_dev/library/system/convert.anubis create mode 100644 anubis_dev/library/test/all_unit_test.anubis create mode 100644 anubis_dev/library/test/system/convert.unit_test.anubis create mode 100644 anubis_dev/library/tools/unit_test.anubis diff --git a/anubis_dev/library/network/tools.anubis b/anubis_dev/library/network/tools.anubis index 3cbfb4e..aa0dc1f 100644 --- a/anubis_dev/library/network/tools.anubis +++ b/anubis_dev/library/network/tools.anubis @@ -86,6 +86,27 @@ public define Int32 b3 = int8_to_int32(force_nth(where , src)), b0 | (b1 << 8) | (b2 << 16) | (b3 << 24). + + + + +public define Int64 + host_to_lendian_Int64 + ( + ByteArray src, + ) = + with b7 = int8_to_int32(force_nth(7, src)), + b6 = int8_to_int32(force_nth(6, src)), + b5 = int8_to_int32(force_nth(5, src)), + b4 = int8_to_int32(force_nth(4, src)), + + b3 = int8_to_int32(force_nth(3, src)), + b2 = int8_to_int32(force_nth(2, src)), + b1 = int8_to_int32(force_nth(1, src)), + b0 = int8_to_int32(force_nth(0, src)), + int64( b0 | (b1 << 8) | (b2 << 16) | (b3 << 24), + b4 | (b5 << 8) | (b6 << 16) | (b7 << 24)). + public define Int16 lendian_to_host_Int16 ( diff --git a/anubis_dev/library/system/convert.anubis b/anubis_dev/library/system/convert.anubis new file mode 100644 index 0000000..efcc4be --- /dev/null +++ b/anubis_dev/library/system/convert.anubis @@ -0,0 +1,57 @@ + +read tools/basis.anubis +read system/types.anubis +read system/string.anubis + +//TODO manage the real endianness of the host + +public define ByteArray + host_to_lendian_Int16_ByteArray + ( + Int16 value, + ) = + with ba = constant_byte_array(2,0), + forget(put(ba,0,value.low)); + forget(put(ba,1,value.high)); + ba. + + +public define ByteArray + host_to_lendian_Int32_ByteArray + ( + Int32 value, + ) = + with ba = constant_byte_array(4, 0), + forget(put(ba,0,truncate_to_int8( value>>24) )); + forget(put(ba,1,truncate_to_int8((value>>16) & 0xFF))); + forget(put(ba,2,truncate_to_int8((value>>8) & 0xFF))); + forget(put(ba,3,truncate_to_int8( value & 0xFF))); + ba. + +define ByteArray + reverse + ( + ByteArray source, + ByteArray dest, + Int32 src_idx, + Int32 dest_idx + ) = + if src_idx < 0 then + dest + else + forget(put(dest, dest_idx, force_nth(src_idx, source))); + reverse(source, dest, src_idx -1, dest_idx +1) + . + +/** Reverse the entire content of the given ByteArray + * if the content is [1|2|3] after reverse it will be [3|2|1] + */ +public define ByteArray + reverse + ( + ByteArray source + )= + with len = length(source), + dest = constant_byte_array(len,0), + reverse(source, dest, len - 1, 0). + diff --git a/anubis_dev/library/system/muscle.anubis b/anubis_dev/library/system/muscle.anubis index 8e4c9e8..efe8c40 100644 --- a/anubis_dev/library/system/muscle.anubis +++ b/anubis_dev/library/system/muscle.anubis @@ -5,6 +5,7 @@ read tools/basis.anubis read network/tools.anubis read system/string.anubis read system/types.anubis +read system/convert.anubis public define Int32 _B_ANY_TYPE = 1095653716. // 'ANYT', // wild card public define Int32 _B_BOOL_TYPE = 1112493900. // 'BOOL', @@ -45,6 +46,7 @@ type Muscle_object: b_int16(Int16), b_int8(Int8), b_message(Message), + b_pointer(Int32), b_point(B_Point), b_rect(B_Rect), b_string(String), @@ -61,7 +63,7 @@ type Muscle_type: b_int16_t, b_int8_t, b_message_t, -/* b_pointer,*/ + b_pointer_t, b_point_t, b_rect_t, b_string_t, @@ -143,18 +145,18 @@ public define One public define Bool is_type_code_variable_size ( - Int32 type_code + Muscle_type type_code ) = - if type_code = _B_BOOL_TYPE | - type_code = _B_DOUBLE_TYPE | - type_code = _B_FLOAT_TYPE | - type_code = _B_INT64_TYPE | - type_code = _B_INT32_TYPE | - type_code = _B_INT16_TYPE | - type_code = _B_INT8_TYPE | - type_code = _B_POINTER_TYPE | - type_code = _B_POINT_TYPE | - type_code = _B_RECT_TYPE then + if type_code = b_bool_t | + type_code = b_double_t | + type_code = b_float_t | + type_code = b_int64_t | + type_code = b_int32_t | + type_code = b_int16_t | + type_code = b_int8_t | + type_code = b_pointer_t | + type_code = b_point_t | + type_code = b_rect_t then true else false. @@ -340,6 +342,17 @@ define Maybe(Muscle_object) success(message) then success(b_message(message)) } + + //read one B_POINTER field. Remark, we read it for compatibility, but itsn't useful in + //Anubis Language, because pointer doesn't exist. + b_pointer_t then + if read_Int32(source) is + { + failure then failure, + success(value) then + print("B_POINTER = " + value + "\n"); + success(b_pointer(value)) //convert into Anubis Int32 + } //read one B_POINT field b_point_t then @@ -565,6 +578,14 @@ define Maybe(Message_field) success(field_items) then success(message_field(field_name, type_code, var(field_items))) } + // read POINTER + b_pointer_t then + if read_field_items(source, type_code, data_length >> 2) is + { + failure then failure, + success(field_items) then success(message_field(field_name, type_code, var(field_items))) + } + // read POINT b_point_t then if read_field_items(source, type_code, data_length >> 3) is @@ -648,7 +669,15 @@ define Maybe(List(Message_field)) } }. - +define Maybe(ByteArray) + flatten_message + ( + Message msg + )= + with flatten_message = host_to_lendian_Int32_ByteArray(_CURRENT_PROTOCOL_VERSION) + + host_to_lendian_Int32_ByteArray(*msg.what), + success(flatten_message). + /* Format: 0. Protocol revision number (4 bytes, always set to CURRENT_PROTOCOL_VERSION) */ /* 1. 'what' code (4 bytes) */ /* 2. Number of entries (4 bytes) */ diff --git a/anubis_dev/library/system/types.anubis b/anubis_dev/library/system/types.anubis index 4079389..744df97 100644 --- a/anubis_dev/library/system/types.anubis +++ b/anubis_dev/library/system/types.anubis @@ -2,6 +2,7 @@ read tools/basis.anubis + public type Int16: int16(Int8 high, Int8 low). @@ -26,3 +27,33 @@ public type Int64: public type Float32: float32(Int32 value). + + +define Bool + compare_all_bytes + ( + ByteArray f, + ByteArray g, + Int32 left + )= + if left < 0 then + true + else + if force_nth(left, f) = force_nth(left, g) then + compare_all_bytes(f, g, left -1) + else + false. + +public define Bool + is_equal + ( + ByteArray f, + ByteArray g + ) = + with len_f = length(f), + len_g = length(g), + if len_f /= len_g then + false + else + compare_all_bytes(f, g, len_f). + diff --git a/anubis_dev/library/test/all_unit_test.anubis b/anubis_dev/library/test/all_unit_test.anubis new file mode 100644 index 0000000..e7b758d --- /dev/null +++ b/anubis_dev/library/test/all_unit_test.anubis @@ -0,0 +1,19 @@ + +read tools/unit_test.anubis +read test/system/convert.unit_test.anubis + + +define List(TestAction) + make_all_tests_list + ( + One dummy + )= + make_convert_test_actions + . + +global define One + all_unit_test + ( + List(String) args + )= + execute_tests(make_all_tests_list(unique)). diff --git a/anubis_dev/library/test/system/convert.unit_test.anubis b/anubis_dev/library/test/system/convert.unit_test.anubis new file mode 100644 index 0000000..ead7a74 --- /dev/null +++ b/anubis_dev/library/test/system/convert.unit_test.anubis @@ -0,0 +1,30 @@ +read tools/basis.anubis +read system/types.anubis +read system/convert.anubis + +read tools/unit_test.anubis + + +define One + byte_array_reverse_test + ( + One dummy + )= + with source = constant_byte_array(3,0), + expected = constant_byte_array(3,0), + forget(put(source,0,1)); + forget(put(source,1,2)); + forget(put(source,2,3)); + + forget(put(expected,0,3)); + forget(put(expected,1,2)); + forget(put(expected,2,1)); + + with result = reverse(source), + assertIsFalse( source = expected); + assertIsTrue( result = expected). + +public define List(TestAction) make_convert_test_actions = +[ + test_action("ByteArray reverse ", byte_array_reverse_test) +]. diff --git a/anubis_dev/library/tools/unit_test.anubis b/anubis_dev/library/tools/unit_test.anubis new file mode 100644 index 0000000..3960b41 --- /dev/null +++ b/anubis_dev/library/tools/unit_test.anubis @@ -0,0 +1,60 @@ +/* + * + * User: David RENE + * Date: 17/01/2007 + * Time: 18:46 + * (c) Otherwise Technology + * + * To change this template use Tools | Options | Coding | Edit Standard Headers. + */ + +read tools/basis.anubis + +public type TestAction: + test_action(String test_name, + One -> One the_test). + + +define One + make_each_test + ( + List(TestAction) tests + ) = + if tests is + { + [] then print("All tests finish"), + [ h . t ] then + print("\nTesting " + h.test_name + "... \n"); + the_test(h)(unique); + make_each_test(t) + }. + +public define One + execute_tests + ( + List(TestAction) tests + )= + make_each_test(tests) + . + +public define One + assertIsTrue + ( + Bool x + ) = + if x then + print("Test OK \n") + else + print("Test NOK \n"). + + +public define One + assertIsFalse + ( + Bool x + ) = + if x then + print("Test NOK \n") + else + print("Test OK \n"). + -- libgit2 0.21.4