Commit 93e3cab8c926e21f1cdbb4eff7b863264b021441
1 parent
a7aa3c37
unit test first release
Showing
7 changed files
with
260 additions
and
13 deletions
Show diff stats
anubis_dev/library/network/tools.anubis
| ... | ... | @@ -86,6 +86,27 @@ public define Int32 |
| 86 | 86 | b3 = int8_to_int32(force_nth(where , src)), |
| 87 | 87 | b0 | (b1 << 8) | (b2 << 16) | (b3 << 24). |
| 88 | 88 | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | +public define Int64 | |
| 94 | + host_to_lendian_Int64 | |
| 95 | + ( | |
| 96 | + ByteArray src, | |
| 97 | + ) = | |
| 98 | + with b7 = int8_to_int32(force_nth(7, src)), | |
| 99 | + b6 = int8_to_int32(force_nth(6, src)), | |
| 100 | + b5 = int8_to_int32(force_nth(5, src)), | |
| 101 | + b4 = int8_to_int32(force_nth(4, src)), | |
| 102 | + | |
| 103 | + b3 = int8_to_int32(force_nth(3, src)), | |
| 104 | + b2 = int8_to_int32(force_nth(2, src)), | |
| 105 | + b1 = int8_to_int32(force_nth(1, src)), | |
| 106 | + b0 = int8_to_int32(force_nth(0, src)), | |
| 107 | + int64( b0 | (b1 << 8) | (b2 << 16) | (b3 << 24), | |
| 108 | + b4 | (b5 << 8) | (b6 << 16) | (b7 << 24)). | |
| 109 | + | |
| 89 | 110 | public define Int16 |
| 90 | 111 | lendian_to_host_Int16 |
| 91 | 112 | ( | ... | ... |
| 1 | + | |
| 2 | +read tools/basis.anubis | |
| 3 | +read system/types.anubis | |
| 4 | +read system/string.anubis | |
| 5 | + | |
| 6 | +//TODO manage the real endianness of the host | |
| 7 | + | |
| 8 | +public define ByteArray | |
| 9 | + host_to_lendian_Int16_ByteArray | |
| 10 | + ( | |
| 11 | + Int16 value, | |
| 12 | + ) = | |
| 13 | + with ba = constant_byte_array(2,0), | |
| 14 | + forget(put(ba,0,value.low)); | |
| 15 | + forget(put(ba,1,value.high)); | |
| 16 | + ba. | |
| 17 | + | |
| 18 | + | |
| 19 | +public define ByteArray | |
| 20 | + host_to_lendian_Int32_ByteArray | |
| 21 | + ( | |
| 22 | + Int32 value, | |
| 23 | + ) = | |
| 24 | + with ba = constant_byte_array(4, 0), | |
| 25 | + forget(put(ba,0,truncate_to_int8( value>>24) )); | |
| 26 | + forget(put(ba,1,truncate_to_int8((value>>16) & 0xFF))); | |
| 27 | + forget(put(ba,2,truncate_to_int8((value>>8) & 0xFF))); | |
| 28 | + forget(put(ba,3,truncate_to_int8( value & 0xFF))); | |
| 29 | + ba. | |
| 30 | + | |
| 31 | +define ByteArray | |
| 32 | + reverse | |
| 33 | + ( | |
| 34 | + ByteArray source, | |
| 35 | + ByteArray dest, | |
| 36 | + Int32 src_idx, | |
| 37 | + Int32 dest_idx | |
| 38 | + ) = | |
| 39 | + if src_idx < 0 then | |
| 40 | + dest | |
| 41 | + else | |
| 42 | + forget(put(dest, dest_idx, force_nth(src_idx, source))); | |
| 43 | + reverse(source, dest, src_idx -1, dest_idx +1) | |
| 44 | + . | |
| 45 | + | |
| 46 | +/** Reverse the entire content of the given ByteArray | |
| 47 | + * if the content is [1|2|3] after reverse it will be [3|2|1] | |
| 48 | + */ | |
| 49 | +public define ByteArray | |
| 50 | + reverse | |
| 51 | + ( | |
| 52 | + ByteArray source | |
| 53 | + )= | |
| 54 | + with len = length(source), | |
| 55 | + dest = constant_byte_array(len,0), | |
| 56 | + reverse(source, dest, len - 1, 0). | |
| 57 | + | ... | ... |
anubis_dev/library/system/muscle.anubis
| ... | ... | @@ -5,6 +5,7 @@ read tools/basis.anubis |
| 5 | 5 | read network/tools.anubis |
| 6 | 6 | read system/string.anubis |
| 7 | 7 | read system/types.anubis |
| 8 | +read system/convert.anubis | |
| 8 | 9 | |
| 9 | 10 | public define Int32 _B_ANY_TYPE = 1095653716. // 'ANYT', // wild card |
| 10 | 11 | public define Int32 _B_BOOL_TYPE = 1112493900. // 'BOOL', |
| ... | ... | @@ -45,6 +46,7 @@ type Muscle_object: |
| 45 | 46 | b_int16(Int16), |
| 46 | 47 | b_int8(Int8), |
| 47 | 48 | b_message(Message), |
| 49 | + b_pointer(Int32), | |
| 48 | 50 | b_point(B_Point), |
| 49 | 51 | b_rect(B_Rect), |
| 50 | 52 | b_string(String), |
| ... | ... | @@ -61,7 +63,7 @@ type Muscle_type: |
| 61 | 63 | b_int16_t, |
| 62 | 64 | b_int8_t, |
| 63 | 65 | b_message_t, |
| 64 | -/* b_pointer,*/ | |
| 66 | + b_pointer_t, | |
| 65 | 67 | b_point_t, |
| 66 | 68 | b_rect_t, |
| 67 | 69 | b_string_t, |
| ... | ... | @@ -143,18 +145,18 @@ public define One |
| 143 | 145 | public define Bool |
| 144 | 146 | is_type_code_variable_size |
| 145 | 147 | ( |
| 146 | - Int32 type_code | |
| 148 | + Muscle_type type_code | |
| 147 | 149 | ) = |
| 148 | - if type_code = _B_BOOL_TYPE | | |
| 149 | - type_code = _B_DOUBLE_TYPE | | |
| 150 | - type_code = _B_FLOAT_TYPE | | |
| 151 | - type_code = _B_INT64_TYPE | | |
| 152 | - type_code = _B_INT32_TYPE | | |
| 153 | - type_code = _B_INT16_TYPE | | |
| 154 | - type_code = _B_INT8_TYPE | | |
| 155 | - type_code = _B_POINTER_TYPE | | |
| 156 | - type_code = _B_POINT_TYPE | | |
| 157 | - type_code = _B_RECT_TYPE then | |
| 150 | + if type_code = b_bool_t | | |
| 151 | + type_code = b_double_t | | |
| 152 | + type_code = b_float_t | | |
| 153 | + type_code = b_int64_t | | |
| 154 | + type_code = b_int32_t | | |
| 155 | + type_code = b_int16_t | | |
| 156 | + type_code = b_int8_t | | |
| 157 | + type_code = b_pointer_t | | |
| 158 | + type_code = b_point_t | | |
| 159 | + type_code = b_rect_t then | |
| 158 | 160 | true |
| 159 | 161 | else |
| 160 | 162 | false. |
| ... | ... | @@ -340,6 +342,17 @@ define Maybe(Muscle_object) |
| 340 | 342 | success(message) then |
| 341 | 343 | success(b_message(message)) |
| 342 | 344 | } |
| 345 | + | |
| 346 | + //read one B_POINTER field. Remark, we read it for compatibility, but itsn't useful in | |
| 347 | + //Anubis Language, because pointer doesn't exist. | |
| 348 | + b_pointer_t then | |
| 349 | + if read_Int32(source) is | |
| 350 | + { | |
| 351 | + failure then failure, | |
| 352 | + success(value) then | |
| 353 | + print("B_POINTER = " + value + "\n"); | |
| 354 | + success(b_pointer(value)) //convert into Anubis Int32 | |
| 355 | + } | |
| 343 | 356 | |
| 344 | 357 | //read one B_POINT field |
| 345 | 358 | b_point_t then |
| ... | ... | @@ -565,6 +578,14 @@ define Maybe(Message_field) |
| 565 | 578 | success(field_items) then success(message_field(field_name, type_code, var(field_items))) |
| 566 | 579 | } |
| 567 | 580 | |
| 581 | + // read POINTER | |
| 582 | + b_pointer_t then | |
| 583 | + if read_field_items(source, type_code, data_length >> 2) is | |
| 584 | + { | |
| 585 | + failure then failure, | |
| 586 | + success(field_items) then success(message_field(field_name, type_code, var(field_items))) | |
| 587 | + } | |
| 588 | + | |
| 568 | 589 | // read POINT |
| 569 | 590 | b_point_t then |
| 570 | 591 | if read_field_items(source, type_code, data_length >> 3) is |
| ... | ... | @@ -648,7 +669,15 @@ define Maybe(List(Message_field)) |
| 648 | 669 | } |
| 649 | 670 | }. |
| 650 | 671 | |
| 651 | - | |
| 672 | +define Maybe(ByteArray) | |
| 673 | + flatten_message | |
| 674 | + ( | |
| 675 | + Message msg | |
| 676 | + )= | |
| 677 | + with flatten_message = host_to_lendian_Int32_ByteArray(_CURRENT_PROTOCOL_VERSION) + | |
| 678 | + host_to_lendian_Int32_ByteArray(*msg.what), | |
| 679 | + success(flatten_message). | |
| 680 | + | |
| 652 | 681 | /* Format: 0. Protocol revision number (4 bytes, always set to CURRENT_PROTOCOL_VERSION) */ |
| 653 | 682 | /* 1. 'what' code (4 bytes) */ |
| 654 | 683 | /* 2. Number of entries (4 bytes) */ | ... | ... |
anubis_dev/library/system/types.anubis
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | read tools/basis.anubis |
| 4 | 4 | |
| 5 | + | |
| 5 | 6 | public type Int16: |
| 6 | 7 | int16(Int8 high, Int8 low). |
| 7 | 8 | |
| ... | ... | @@ -26,3 +27,33 @@ public type Int64: |
| 26 | 27 | |
| 27 | 28 | public type Float32: |
| 28 | 29 | float32(Int32 value). |
| 30 | + | |
| 31 | + | |
| 32 | +define Bool | |
| 33 | + compare_all_bytes | |
| 34 | + ( | |
| 35 | + ByteArray f, | |
| 36 | + ByteArray g, | |
| 37 | + Int32 left | |
| 38 | + )= | |
| 39 | + if left < 0 then | |
| 40 | + true | |
| 41 | + else | |
| 42 | + if force_nth(left, f) = force_nth(left, g) then | |
| 43 | + compare_all_bytes(f, g, left -1) | |
| 44 | + else | |
| 45 | + false. | |
| 46 | + | |
| 47 | +public define Bool | |
| 48 | + is_equal | |
| 49 | + ( | |
| 50 | + ByteArray f, | |
| 51 | + ByteArray g | |
| 52 | + ) = | |
| 53 | + with len_f = length(f), | |
| 54 | + len_g = length(g), | |
| 55 | + if len_f /= len_g then | |
| 56 | + false | |
| 57 | + else | |
| 58 | + compare_all_bytes(f, g, len_f). | |
| 59 | + | ... | ... |
| 1 | + | |
| 2 | +read tools/unit_test.anubis | |
| 3 | +read test/system/convert.unit_test.anubis | |
| 4 | + | |
| 5 | + | |
| 6 | +define List(TestAction) | |
| 7 | + make_all_tests_list | |
| 8 | + ( | |
| 9 | + One dummy | |
| 10 | + )= | |
| 11 | + make_convert_test_actions | |
| 12 | + . | |
| 13 | + | |
| 14 | +global define One | |
| 15 | + all_unit_test | |
| 16 | + ( | |
| 17 | + List(String) args | |
| 18 | + )= | |
| 19 | + execute_tests(make_all_tests_list(unique)). | ... | ... |
anubis_dev/library/test/system/convert.unit_test.anubis
0 → 100644
| 1 | +read tools/basis.anubis | |
| 2 | +read system/types.anubis | |
| 3 | +read system/convert.anubis | |
| 4 | + | |
| 5 | +read tools/unit_test.anubis | |
| 6 | + | |
| 7 | + | |
| 8 | +define One | |
| 9 | + byte_array_reverse_test | |
| 10 | + ( | |
| 11 | + One dummy | |
| 12 | + )= | |
| 13 | + with source = constant_byte_array(3,0), | |
| 14 | + expected = constant_byte_array(3,0), | |
| 15 | + forget(put(source,0,1)); | |
| 16 | + forget(put(source,1,2)); | |
| 17 | + forget(put(source,2,3)); | |
| 18 | + | |
| 19 | + forget(put(expected,0,3)); | |
| 20 | + forget(put(expected,1,2)); | |
| 21 | + forget(put(expected,2,1)); | |
| 22 | + | |
| 23 | + with result = reverse(source), | |
| 24 | + assertIsFalse( source = expected); | |
| 25 | + assertIsTrue( result = expected). | |
| 26 | + | |
| 27 | +public define List(TestAction) make_convert_test_actions = | |
| 28 | +[ | |
| 29 | + test_action("ByteArray reverse ", byte_array_reverse_test) | |
| 30 | +]. | ... | ... |
| 1 | +/* | |
| 2 | + * | |
| 3 | + * User: David RENE | |
| 4 | + * Date: 17/01/2007 | |
| 5 | + * Time: 18:46 | |
| 6 | + * (c) Otherwise Technology | |
| 7 | + * | |
| 8 | + * To change this template use Tools | Options | Coding | Edit Standard Headers. | |
| 9 | + */ | |
| 10 | + | |
| 11 | +read tools/basis.anubis | |
| 12 | + | |
| 13 | +public type TestAction: | |
| 14 | + test_action(String test_name, | |
| 15 | + One -> One the_test). | |
| 16 | + | |
| 17 | + | |
| 18 | +define One | |
| 19 | + make_each_test | |
| 20 | + ( | |
| 21 | + List(TestAction) tests | |
| 22 | + ) = | |
| 23 | + if tests is | |
| 24 | + { | |
| 25 | + [] then print("All tests finish"), | |
| 26 | + [ h . t ] then | |
| 27 | + print("\nTesting " + h.test_name + "... \n"); | |
| 28 | + the_test(h)(unique); | |
| 29 | + make_each_test(t) | |
| 30 | + }. | |
| 31 | + | |
| 32 | +public define One | |
| 33 | + execute_tests | |
| 34 | + ( | |
| 35 | + List(TestAction) tests | |
| 36 | + )= | |
| 37 | + make_each_test(tests) | |
| 38 | + . | |
| 39 | + | |
| 40 | +public define One | |
| 41 | + assertIsTrue | |
| 42 | + ( | |
| 43 | + Bool x | |
| 44 | + ) = | |
| 45 | + if x then | |
| 46 | + print("Test OK \n") | |
| 47 | + else | |
| 48 | + print("Test NOK \n"). | |
| 49 | + | |
| 50 | + | |
| 51 | +public define One | |
| 52 | + assertIsFalse | |
| 53 | + ( | |
| 54 | + Bool x | |
| 55 | + ) = | |
| 56 | + if x then | |
| 57 | + print("Test NOK \n") | |
| 58 | + else | |
| 59 | + print("Test OK \n"). | |
| 60 | + | ... | ... |