Librairie xbee.
Dependents: NerfUS-Coord NerfUSTarget
Fork of APP3_xbee by
tests/test.cpp@24:00c42ba87ef7, 2017-04-05 (annotated)
- Committer:
- GaiSensei
- Date:
- Wed Apr 05 21:09:32 2017 +0000
- Revision:
- 24:00c42ba87ef7
- Parent:
- 23:2b67589150d9
Envoyer a une adresse compile
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GaiSensei | 17:969aa15f783d | 1 | ///////////////////////////////////////////////////////////// |
GaiSensei | 17:969aa15f783d | 2 | // APP 3 // |
GaiSensei | 17:969aa15f783d | 3 | // // |
GaiSensei | 17:969aa15f783d | 4 | // Université de Sherbrooke // |
GaiSensei | 17:969aa15f783d | 5 | // Génie informatique // |
GaiSensei | 17:969aa15f783d | 6 | // Session 5, Hiver 2017 // |
GaiSensei | 17:969aa15f783d | 7 | // // |
GaiSensei | 17:969aa15f783d | 8 | // Date: 14 février 2017 // |
GaiSensei | 17:969aa15f783d | 9 | // // |
GaiSensei | 17:969aa15f783d | 10 | // Auteurs: Maxime Dupuis, dupm2216 // |
GaiSensei | 17:969aa15f783d | 11 | // Bruno Allaire-Lemay, allb2701 // |
GaiSensei | 17:969aa15f783d | 12 | ///////////////////////////////////////////////////////////// |
GaiSensei | 17:969aa15f783d | 13 | |
GaiSensei | 17:969aa15f783d | 14 | #include <cassert> |
GaiSensei | 17:969aa15f783d | 15 | #include <algorithm> |
GaiSensei | 17:969aa15f783d | 16 | #include "test.h" |
GaiSensei | 17:969aa15f783d | 17 | #include "xbee.h" |
GaiSensei | 17:969aa15f783d | 18 | |
GaiSensei | 17:969aa15f783d | 19 | void run_all_tests() |
GaiSensei | 17:969aa15f783d | 20 | { |
GaiSensei | 17:969aa15f783d | 21 | vectors_are_equal_test(); |
GaiSensei | 17:969aa15f783d | 22 | generate_transmit_request_test(); |
GaiSensei | 17:969aa15f783d | 23 | generate_led_high_command_test(); |
GaiSensei | 17:969aa15f783d | 24 | generate_led_low_command_test(); |
GaiSensei | 17:969aa15f783d | 25 | parse_receive_packet_test(); |
GaiSensei | 17:969aa15f783d | 26 | parse_frame_parses_receive_packet_test(); |
GaiSensei | 17:969aa15f783d | 27 | parse_frame_parses_transmit_status_test(); |
GaiSensei | 17:969aa15f783d | 28 | parse_frame_parses_at_command_response_test(); |
GaiSensei | 17:969aa15f783d | 29 | parse_frame_parses_remote_command_response_test(); |
GaiSensei | 17:969aa15f783d | 30 | handle_frame_adds_parsed_frame_to_mailbox_test(); |
GaiSensei | 17:969aa15f783d | 31 | |
GaiSensei | 17:969aa15f783d | 32 | test_create_vector_by_copy_test(); |
GaiSensei | 17:969aa15f783d | 33 | |
GaiSensei | 17:969aa15f783d | 34 | string_to_data_test(); |
GaiSensei | 17:969aa15f783d | 35 | hexa_char_to_dec_test(); |
GaiSensei | 17:969aa15f783d | 36 | } |
GaiSensei | 17:969aa15f783d | 37 | |
GaiSensei | 17:969aa15f783d | 38 | void vectors_are_equal_test() |
GaiSensei | 17:969aa15f783d | 39 | { |
GaiSensei | 17:969aa15f783d | 40 | vector<char> v1; |
GaiSensei | 17:969aa15f783d | 41 | v1.push_back(1); |
GaiSensei | 17:969aa15f783d | 42 | v1.push_back(2); |
GaiSensei | 17:969aa15f783d | 43 | |
GaiSensei | 17:969aa15f783d | 44 | vector<char> v2; |
GaiSensei | 17:969aa15f783d | 45 | v2.push_back(1); |
GaiSensei | 17:969aa15f783d | 46 | v2.push_back(2); |
GaiSensei | 17:969aa15f783d | 47 | |
GaiSensei | 17:969aa15f783d | 48 | vector<char> v3; |
GaiSensei | 17:969aa15f783d | 49 | v3.push_back(1); |
GaiSensei | 17:969aa15f783d | 50 | v3.push_back(2); |
GaiSensei | 17:969aa15f783d | 51 | v3.push_back(3); |
GaiSensei | 17:969aa15f783d | 52 | |
GaiSensei | 17:969aa15f783d | 53 | vector<char> v4; |
GaiSensei | 17:969aa15f783d | 54 | v4.push_back(1); |
GaiSensei | 17:969aa15f783d | 55 | v4.push_back(1); |
GaiSensei | 17:969aa15f783d | 56 | |
GaiSensei | 17:969aa15f783d | 57 | assert(vectors_are_equal(v1, v2)); |
GaiSensei | 17:969aa15f783d | 58 | |
GaiSensei | 17:969aa15f783d | 59 | assert(!vectors_are_equal(v1, v3)); |
GaiSensei | 17:969aa15f783d | 60 | assert(!vectors_are_equal(v1, v4)); |
GaiSensei | 17:969aa15f783d | 61 | } |
GaiSensei | 17:969aa15f783d | 62 | |
GaiSensei | 17:969aa15f783d | 63 | bool vectors_are_equal(const vector<char> v1, const vector<char> v2) |
GaiSensei | 17:969aa15f783d | 64 | { |
GaiSensei | 17:969aa15f783d | 65 | if(v1.size() != v2.size()) return false; |
GaiSensei | 17:969aa15f783d | 66 | |
GaiSensei | 17:969aa15f783d | 67 | const bool all_elements_are_equal = equal(v1.begin(), v1.end(), v2.begin()); |
GaiSensei | 17:969aa15f783d | 68 | return all_elements_are_equal; |
GaiSensei | 17:969aa15f783d | 69 | } |
GaiSensei | 17:969aa15f783d | 70 | |
GaiSensei | 17:969aa15f783d | 71 | vector<char> construct_vector(const char* content, int length) |
GaiSensei | 17:969aa15f783d | 72 | { |
GaiSensei | 17:969aa15f783d | 73 | vector<char> constructed_vector; |
GaiSensei | 17:969aa15f783d | 74 | for(int i = 0; i < length; ++i) |
GaiSensei | 17:969aa15f783d | 75 | { |
GaiSensei | 17:969aa15f783d | 76 | constructed_vector.push_back(content[i]); |
GaiSensei | 17:969aa15f783d | 77 | } |
GaiSensei | 17:969aa15f783d | 78 | return constructed_vector; |
GaiSensei | 17:969aa15f783d | 79 | } |
GaiSensei | 17:969aa15f783d | 80 | |
GaiSensei | 17:969aa15f783d | 81 | int some_function_that_returns_int_forty_two() |
GaiSensei | 17:969aa15f783d | 82 | { |
GaiSensei | 17:969aa15f783d | 83 | return 42; |
GaiSensei | 17:969aa15f783d | 84 | } |
GaiSensei | 17:969aa15f783d | 85 | |
GaiSensei | 17:969aa15f783d | 86 | void test_create_vector_by_copy_test() |
GaiSensei | 17:969aa15f783d | 87 | { |
GaiSensei | 17:969aa15f783d | 88 | const vector<char> original(2, 3); |
GaiSensei | 17:969aa15f783d | 89 | vector<char> *copy_vector = new vector<char>(); |
GaiSensei | 17:969aa15f783d | 90 | *copy_vector = original; |
GaiSensei | 17:969aa15f783d | 91 | |
GaiSensei | 17:969aa15f783d | 92 | assert(vectors_are_equal(original, *copy_vector)); |
GaiSensei | 17:969aa15f783d | 93 | |
GaiSensei | 17:969aa15f783d | 94 | delete copy_vector; |
GaiSensei | 17:969aa15f783d | 95 | |
GaiSensei | 17:969aa15f783d | 96 | assert(original.at(0) == 3); |
GaiSensei | 17:969aa15f783d | 97 | assert(original.at(1) == 3); |
GaiSensei | 17:969aa15f783d | 98 | } |
GaiSensei | 17:969aa15f783d | 99 | |
GaiSensei | 17:969aa15f783d | 100 | void generate_transmit_request_test() |
GaiSensei | 17:969aa15f783d | 101 | { |
GaiSensei | 21:441645a394c2 | 102 | const char expected_array[] = {0x7E, 0x00, 0x12, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x54, 0x65, 0x73, 0x74, 0x53}; |
GaiSensei | 17:969aa15f783d | 103 | vector<char> expected_request = construct_vector(expected_array, 22); |
GaiSensei | 17:969aa15f783d | 104 | |
GaiSensei | 24:00c42ba87ef7 | 105 | int address[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF}; |
GaiSensei | 23:2b67589150d9 | 106 | |
GaiSensei | 23:2b67589150d9 | 107 | vector<char> actual_request = generate_transmit_request("Test", 4, address); |
GaiSensei | 17:969aa15f783d | 108 | |
GaiSensei | 17:969aa15f783d | 109 | assert(vectors_are_equal(actual_request, expected_request)); |
GaiSensei | 17:969aa15f783d | 110 | } |
GaiSensei | 17:969aa15f783d | 111 | |
GaiSensei | 17:969aa15f783d | 112 | void generate_led_high_command_test() |
GaiSensei | 17:969aa15f783d | 113 | { |
GaiSensei | 17:969aa15f783d | 114 | const char expected_array[] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFE, 0x02, 0x50, 0x32, 0x05, 0x63}; |
GaiSensei | 17:969aa15f783d | 115 | vector<char> expected_request = construct_vector(expected_array, 20); |
GaiSensei | 17:969aa15f783d | 116 | |
GaiSensei | 17:969aa15f783d | 117 | vector<char> actual_request = generate_led_command(true); |
GaiSensei | 17:969aa15f783d | 118 | |
GaiSensei | 17:969aa15f783d | 119 | assert(vectors_are_equal(actual_request, expected_request)); |
GaiSensei | 17:969aa15f783d | 120 | } |
GaiSensei | 17:969aa15f783d | 121 | |
GaiSensei | 17:969aa15f783d | 122 | void generate_led_low_command_test() |
GaiSensei | 17:969aa15f783d | 123 | { |
GaiSensei | 17:969aa15f783d | 124 | const char expected_array[] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFE, 0x02, 0x50, 0x32, 0x04, 0x64}; |
GaiSensei | 17:969aa15f783d | 125 | vector<char> expected_request = construct_vector(expected_array, 20); |
GaiSensei | 17:969aa15f783d | 126 | |
GaiSensei | 17:969aa15f783d | 127 | vector<char> actual_request = generate_led_command(false); |
GaiSensei | 17:969aa15f783d | 128 | |
GaiSensei | 17:969aa15f783d | 129 | assert(vectors_are_equal(actual_request, expected_request)); |
GaiSensei | 17:969aa15f783d | 130 | } |
GaiSensei | 17:969aa15f783d | 131 | |
GaiSensei | 17:969aa15f783d | 132 | void parse_receive_packet_test() |
GaiSensei | 17:969aa15f783d | 133 | { |
GaiSensei | 17:969aa15f783d | 134 | const char receive_packet_array[] = {0x7E, 0x00, 0x10, 0x90, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x86, 0xDA, 0x0E, 0x90, 0x1D, 0x01, 0x54, 0x65, 0x73, 0x74, 0xBE}; |
GaiSensei | 17:969aa15f783d | 135 | vector<char> receive_packet = construct_vector(receive_packet_array, 20); |
GaiSensei | 17:969aa15f783d | 136 | |
GaiSensei | 17:969aa15f783d | 137 | const char expected_message_array[] = {0x54, 0x65, 0x73, 0x74}; |
GaiSensei | 17:969aa15f783d | 138 | vector<char> expected_message = construct_vector(expected_message_array, 4); |
GaiSensei | 17:969aa15f783d | 139 | |
GaiSensei | 17:969aa15f783d | 140 | vector<char> actual_message = parse_receive_packet(receive_packet); |
GaiSensei | 17:969aa15f783d | 141 | |
GaiSensei | 17:969aa15f783d | 142 | assert(vectors_are_equal(actual_message, expected_message)); |
GaiSensei | 17:969aa15f783d | 143 | } |
GaiSensei | 17:969aa15f783d | 144 | |
GaiSensei | 17:969aa15f783d | 145 | void parse_frame_parses_receive_packet_test() |
GaiSensei | 17:969aa15f783d | 146 | { |
GaiSensei | 17:969aa15f783d | 147 | const char frame_to_parse_array[] = {0x7E, 0x00, 0x10, 0x90, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x86, 0xDA, 0x0E, 0x90, 0x1D, 0x01, 0x54, 0x65, 0x73, 0x74, 0xBE}; |
GaiSensei | 17:969aa15f783d | 148 | const vector<char> frame_to_parse = construct_vector(frame_to_parse_array, 20); |
GaiSensei | 17:969aa15f783d | 149 | |
GaiSensei | 17:969aa15f783d | 150 | const char expected_parsed_frame_array[] = {FRAME_TYPE_RECEIVE_PACKET, 'T', 'e', 's', 't'}; |
GaiSensei | 17:969aa15f783d | 151 | const vector<char> expected_parsed_frame = construct_vector(expected_parsed_frame_array, 5); |
GaiSensei | 17:969aa15f783d | 152 | |
GaiSensei | 17:969aa15f783d | 153 | const vector<char> actual_parsed_frame = parse_frame(frame_to_parse); |
GaiSensei | 17:969aa15f783d | 154 | |
GaiSensei | 17:969aa15f783d | 155 | assert(vectors_are_equal(expected_parsed_frame, actual_parsed_frame)); |
GaiSensei | 17:969aa15f783d | 156 | } |
GaiSensei | 17:969aa15f783d | 157 | |
GaiSensei | 17:969aa15f783d | 158 | void parse_frame_parses_transmit_status_test() |
GaiSensei | 17:969aa15f783d | 159 | { |
GaiSensei | 17:969aa15f783d | 160 | const char frame_to_parse_array[] = {0x7E, 0x00, 0x07, 0x8B, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73}; |
GaiSensei | 17:969aa15f783d | 161 | const vector<char> frame_to_parse = construct_vector(frame_to_parse_array, 11); |
GaiSensei | 17:969aa15f783d | 162 | |
GaiSensei | 17:969aa15f783d | 163 | const char expected_parsed_frame_array[] = {FRAME_TYPE_TRANSMIT_STATUS, TRANSMIT_STATUS_SUCCESS}; |
GaiSensei | 17:969aa15f783d | 164 | const vector<char> expected_parsed_frame = construct_vector(expected_parsed_frame_array, 2); |
GaiSensei | 17:969aa15f783d | 165 | |
GaiSensei | 17:969aa15f783d | 166 | const vector<char> actual_parsed_frame = parse_frame(frame_to_parse); |
GaiSensei | 17:969aa15f783d | 167 | |
GaiSensei | 17:969aa15f783d | 168 | assert(vectors_are_equal(expected_parsed_frame, actual_parsed_frame)); |
GaiSensei | 17:969aa15f783d | 169 | } |
GaiSensei | 17:969aa15f783d | 170 | |
GaiSensei | 17:969aa15f783d | 171 | void parse_frame_parses_at_command_response_test() |
GaiSensei | 17:969aa15f783d | 172 | { |
GaiSensei | 17:969aa15f783d | 173 | const char frame_to_parse_array[] = {0x7E, 0x00, 0x05, 0x88, 0x01, 0x4E, 0x4A, 0x00, 0xDE}; |
GaiSensei | 17:969aa15f783d | 174 | const vector<char> frame_to_parse = construct_vector(frame_to_parse_array, 9); |
GaiSensei | 17:969aa15f783d | 175 | |
GaiSensei | 17:969aa15f783d | 176 | const char expected_parsed_frame_array[] = {FRAME_TYPE_AT_COMMAND_RESPONSE, AT_COMMAND_RESPONSE_STATUS_OK}; |
GaiSensei | 17:969aa15f783d | 177 | const vector<char> expected_parsed_frame = construct_vector(expected_parsed_frame_array, 2); |
GaiSensei | 17:969aa15f783d | 178 | |
GaiSensei | 17:969aa15f783d | 179 | const vector<char> actual_parsed_frame = parse_frame(frame_to_parse); |
GaiSensei | 17:969aa15f783d | 180 | |
GaiSensei | 17:969aa15f783d | 181 | assert(vectors_are_equal(expected_parsed_frame, actual_parsed_frame)); |
GaiSensei | 17:969aa15f783d | 182 | } |
GaiSensei | 17:969aa15f783d | 183 | |
GaiSensei | 17:969aa15f783d | 184 | void parse_frame_parses_remote_command_response_test() |
GaiSensei | 17:969aa15f783d | 185 | { |
GaiSensei | 17:969aa15f783d | 186 | const char frame_to_parse_array[] = {0x7E, 0x00, 0x0F, 0x97, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x86, 0xDA, 0x0E, 0x90, 0x1D, 0x50, 0x32, 0x00, 0xD5}; |
GaiSensei | 17:969aa15f783d | 187 | const vector<char> frame_to_parse = construct_vector(frame_to_parse_array, 19); |
GaiSensei | 17:969aa15f783d | 188 | |
GaiSensei | 17:969aa15f783d | 189 | const char expected_parsed_frame_array[] = {FRAME_TYPE_REMOTE_COMMAND_RESPONSE, REMOTE_COMMAND_RESPONSE_COMMAND_STATUS_OK}; |
GaiSensei | 17:969aa15f783d | 190 | const vector<char> expected_parsed_frame = construct_vector(expected_parsed_frame_array, 2); |
GaiSensei | 17:969aa15f783d | 191 | |
GaiSensei | 17:969aa15f783d | 192 | const vector<char> actual_parsed_frame = parse_frame(frame_to_parse); |
GaiSensei | 17:969aa15f783d | 193 | |
GaiSensei | 17:969aa15f783d | 194 | assert(vectors_are_equal(expected_parsed_frame, actual_parsed_frame)); |
GaiSensei | 17:969aa15f783d | 195 | } |
GaiSensei | 17:969aa15f783d | 196 | |
GaiSensei | 17:969aa15f783d | 197 | void handle_frame_adds_parsed_frame_to_mailbox_test() |
GaiSensei | 17:969aa15f783d | 198 | { |
GaiSensei | 17:969aa15f783d | 199 | const char frame_to_handle_array[] = {0x7E, 0x00, 0x05, 0x88, 0x01, 0x4E, 0x4A, 0x00, 0xDE}; |
GaiSensei | 17:969aa15f783d | 200 | const vector<char> frame_to_handle = construct_vector(frame_to_handle_array, 9); |
GaiSensei | 17:969aa15f783d | 201 | |
GaiSensei | 17:969aa15f783d | 202 | const char expected_parsed_frame_array[] = {FRAME_TYPE_AT_COMMAND_RESPONSE, AT_COMMAND_RESPONSE_STATUS_OK}; |
GaiSensei | 17:969aa15f783d | 203 | const vector<char> expected_parsed_frame = construct_vector(expected_parsed_frame_array, 2); |
GaiSensei | 17:969aa15f783d | 204 | |
GaiSensei | 17:969aa15f783d | 205 | handle_frame(frame_to_handle); |
GaiSensei | 17:969aa15f783d | 206 | |
GaiSensei | 17:969aa15f783d | 207 | //Read parsed frame from the mailbox |
GaiSensei | 17:969aa15f783d | 208 | osEvent event = parsed_frames.get(); |
GaiSensei | 17:969aa15f783d | 209 | assert(event.status == osEventMail); |
GaiSensei | 17:969aa15f783d | 210 | ingoing_value_t *parsed_frame = (ingoing_value_t*)event.value.p; |
GaiSensei | 17:969aa15f783d | 211 | |
GaiSensei | 17:969aa15f783d | 212 | assert(vectors_are_equal(expected_parsed_frame, ingoing_value_to_vector(*parsed_frame))); |
GaiSensei | 17:969aa15f783d | 213 | |
GaiSensei | 17:969aa15f783d | 214 | parsed_frames.free(parsed_frame); |
GaiSensei | 17:969aa15f783d | 215 | } |
GaiSensei | 17:969aa15f783d | 216 | |
GaiSensei | 17:969aa15f783d | 217 | void string_to_data_test() |
GaiSensei | 17:969aa15f783d | 218 | { |
GaiSensei | 17:969aa15f783d | 219 | vector<char> result; |
GaiSensei | 17:969aa15f783d | 220 | |
GaiSensei | 17:969aa15f783d | 221 | result = string_to_data("0000"); |
GaiSensei | 17:969aa15f783d | 222 | assert(result.at(0) == 0); |
GaiSensei | 17:969aa15f783d | 223 | assert(result.at(1) == 0); |
GaiSensei | 17:969aa15f783d | 224 | |
GaiSensei | 17:969aa15f783d | 225 | result = string_to_data("EEEEEE"); |
GaiSensei | 17:969aa15f783d | 226 | assert(result.at(0) == 0xEE); |
GaiSensei | 17:969aa15f783d | 227 | assert(result.at(1) == 0xEE); |
GaiSensei | 17:969aa15f783d | 228 | } |
GaiSensei | 17:969aa15f783d | 229 | |
GaiSensei | 17:969aa15f783d | 230 | void hexa_char_to_dec_test() |
GaiSensei | 17:969aa15f783d | 231 | { |
GaiSensei | 17:969aa15f783d | 232 | assert(hexa_char_to_dec('0') == 0); |
GaiSensei | 17:969aa15f783d | 233 | assert(hexa_char_to_dec('1') == 1); |
GaiSensei | 17:969aa15f783d | 234 | assert(hexa_char_to_dec('A') == 10); |
GaiSensei | 17:969aa15f783d | 235 | assert(hexa_char_to_dec('F') == 15); |
GaiSensei | 17:969aa15f783d | 236 | |
GaiSensei | 17:969aa15f783d | 237 | assert(hexa_char_to_dec('a') == 10); |
GaiSensei | 17:969aa15f783d | 238 | assert(hexa_char_to_dec('f') == 15); |
GaiSensei | 17:969aa15f783d | 239 | } |