Changeset 556:2d1a26628a09
- Timestamp:
- 06/01/08 07:22:00 (7 months ago)
- Branch:
- default
- Location:
- nxos
- Files:
-
- 8 modified
-
base/_fs.c (modified) (1 diff)
-
base/drivers/radar.c (modified) (1 diff)
-
base/lib/rcmd/_rcmd.c (modified) (2 diffs)
-
base/util.c (modified) (1 diff)
-
base/util.h (modified) (1 diff)
-
systems/tests/main.c (modified) (1 diff)
-
systems/tests/tests.c (modified) (12 diffs)
-
systems/tests/tests.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
nxos/base/_fs.c
r551 r556 54 54 FS_FILENAME_LENGTH); 55 55 56 if (str cmp(nameconv.chars, name) == 0) {56 if (streq(nameconv.chars, name)) { 57 57 *origin = i; 58 58 return FS_ERR_NO_ERROR; -
nxos/base/drivers/radar.c
r499 r556 95 95 96 96 return nx_radar_read(sensor, RADAR_SENSOR_TYPE, type) 97 && strncmp((char *)type, RADAR_LEGO_SENSOR_TYPE, 98 strlen(RADAR_LEGO_SENSOR_TYPE)) == 0; 97 && streq((char *)type, RADAR_LEGO_SENSOR_TYPE); 99 98 } 100 99 -
nxos/base/lib/rcmd/_rcmd.c
r551 r556 40 40 41 41 while (rcmd_commands[i].name) { 42 if (strncmp(line, rcmd_commands[i].name, 43 strlen(rcmd_commands[i].name)) == 0) { 42 if (streq(line, rcmd_commands[i].name)) { 44 43 *command = rcmd_commands[i]; 45 44 return RCMD_ERR_NO_ERROR; … … 165 164 } 166 165 167 if (ntokens == 4 && str ncmp(line + indices[3], "sync", 4) == 0) {166 if (ntokens == 4 && streq(line + indices[3], "sync") == 0) { 168 167 nx_sound_freq(freq, duration); 169 168 } else { -
nxos/base/util.c
r554 r556 44 44 } 45 45 46 U32 strncmp(const char *a, const char *b, U32 n) { 47 U32 i; 48 46 bool streqn(const char *a, const char *b, U32 n) { 49 47 NX_ASSERT(a != NULL && b != NULL); 50 48 51 for (i=0 ; i<n ; i++) { 52 if (a[i] < b[i]) { 53 return -1; 54 } else if (a[i] > b[i]) { 55 return 1; 56 } 49 while (n--) { 50 if (*a != *b++) 51 return FALSE; 52 if (*a++ == '\0') 53 break; 57 54 } 58 55 59 return 0;56 return TRUE; 60 57 } 61 58 62 U32 strcmp(const char *a, const char *b) { 63 return strncmp(a, b, MIN(strlen(a), strlen(b)+1)); 59 bool streq(const char *a, const char *b) { 60 NX_ASSERT(a != NULL && b != NULL); 61 62 while (*a != '\0' && *b != '\0') { 63 if (*a++ != *b++) 64 return FALSE; 65 } 66 67 return *a == *b ? TRUE : FALSE; 64 68 } 65 69 -
nxos/base/util.h
r547 r556 58 58 U32 strlen(const char *str); 59 59 60 /** Order two strings based on a limited amount of input.60 /** Compare two string prefixes for equality. 61 61 * 62 62 * @param a First string to compare. 63 63 * @param b Second string to compare. 64 64 * @param n Number of bytes to compare. 65 * @return -1 if @a a sorts before @a b, 1 if @a b sorts before @a a, 0 66 * if both are equivalent up to @a n bytes. 65 * @return TRUE if the first @a n bytes of @a a are equal to @a b, 66 * FALSE otherwise. 67 * 68 * @note This function will halt on the first NULL byte it finds in 69 * either string. 67 70 */ 68 U32 strncmp(const char *a, const char *b, U32 n);71 bool streqn(const char *a, const char *b, U32 n); 69 72 70 /** Order two strings.73 /** compare two strings for equality. 71 74 * 72 * This is equivalent to @a strn cmp, with the maximum length being the75 * This is equivalent to @a strneq, with the maximum length being the 73 76 * length of the shortest input string. 74 77 * 75 * @see strn cmp78 * @see strneq 76 79 */ 77 U32 strcmp(const char *a, const char *b);80 bool streq(const char *a, const char *b); 78 81 79 82 /** Locate leftmost instance of character @a c in string @a s. -
nxos/systems/tests/main.c
r518 r556 19 19 20 20 //tests_all(); 21 tests_usb();21 //tests_usb(); 22 22 //tests_bt(); 23 23 //tests_usb_hardcore(); -
nxos/systems/tests/tests.c
r502 r556 4 4 #include "base/interrupts.h" 5 5 #include "base/display.h" 6 #include "base/assert.h" 6 7 #include "base/memmap.h" 7 8 #include "base/util.h" … … 42 43 } 43 44 45 46 void tests_util(void) { 47 hello(); 48 49 /* Simple equality. */ 50 NX_ASSERT(streq("foo", "foo")); 51 52 /* Simple inequality. */ 53 NX_ASSERT(!streq("foo", "bar")); 54 NX_ASSERT(!streq("bar", "foo")); 55 56 /* Inequality towards the end of the string. */ 57 NX_ASSERT(!streq("foo", "fob")); 58 NX_ASSERT(!streq("fob", "foo")); 59 60 /* Inequality of different length strings. */ 61 NX_ASSERT(!streq("foo", "foobar")); 62 NX_ASSERT(!streq("foobar", "foo")); 63 64 /* Inequality vs. the empty string. */ 65 NX_ASSERT(!streq("foo", "")); 66 NX_ASSERT(!streq("", "foo")); 67 68 /* The border case of the empty string. */ 69 NX_ASSERT(streq("", "")); 70 71 72 /* Simple equality. */ 73 NX_ASSERT(streqn("foo", "foo", 3)); 74 75 /* Simple inequality. */ 76 NX_ASSERT(!streqn("foo", "bar", 3)); 77 NX_ASSERT(!streqn("bar", "foo", 3)); 78 79 /* Inequality towards the end of the string. */ 80 NX_ASSERT(!streqn("foo", "fob", 3)); 81 NX_ASSERT(!streqn("fob", "foo", 3)); 82 83 /* Inequality of different length strings. */ 84 NX_ASSERT(!streqn("foo", "foobar", 6)); 85 NX_ASSERT(!streqn("foobar", "foo", 6)); 86 87 /* Inequality vs. the empty string. */ 88 NX_ASSERT(!streqn("foo", "", 3)); 89 NX_ASSERT(!streqn("", "foo", 3)); 90 91 /* Equality of the empty string, no matter the given length. */ 92 NX_ASSERT(streqn("", "", 42)); 93 94 /* Equality of unequal strings if length == 0 */ 95 NX_ASSERT(streqn("bleh", "foo", 0)); 96 97 /* Prefix equality of unequal strings */ 98 NX_ASSERT(streqn("feh", "foo", 1)); 99 100 goodbye(); 101 } 102 103 44 104 void tests_display(void) { 45 105 char buf[2] = { 0, 0 }; … … 327 387 } 328 388 329 void tests_bt2(void) 330 { 389 void tests_bt2(void) { 331 390 /*int i; 332 391 */ … … 387 446 388 447 389 /* returns 1 if they are identic390 * 0 else391 *392 * TODO: use base/util.h:strncmp instead.393 */394 static U8 compare_str(char *str_a, char *str_b, U32 max)395 {396 397 while (*str_a != '\0'398 && *str_b != '\0'399 && max > 0)400 {401 if (*str_a != *str_b) {402 return 0;403 }404 str_a++;405 str_b++;406 max--;407 }408 409 if (*str_a != *str_b && max > 0) {410 return 0;411 }412 413 return 1;414 }415 416 417 448 #define CMD_UNKNOWN "Unknown" 418 449 #define CMD_OK "Ok" … … 426 457 * @return 0 if success ; 1 if unknown command ; 2 if halt 427 458 */ 428 static int tests_command(char *buffer, int lng) 429 { 459 static int tests_command(char *buffer) { 430 460 int i; 431 461 S32 t; … … 434 464 435 465 i = 0; 436 if ( compare_str(buffer, "motor", lng))466 if (streq(buffer, "motor")) 437 467 tests_motor(); 438 else if ( compare_str(buffer, "sound", lng))468 else if (streq(buffer, "sound")) 439 469 tests_sound(); 440 else if (compare_str(buffer, "display", lng)) 470 else if (streq(buffer, "util")) 471 tests_util(); 472 else if (streq(buffer, "display")) 441 473 tests_display(); 442 else if ( compare_str(buffer, "sysinfo", lng))474 else if (streq(buffer, "sysinfo")) 443 475 tests_sysinfo(); 444 else if ( compare_str(buffer, "sensors", lng))476 else if (streq(buffer, "sensors")) 445 477 tests_sensors(); 446 else if ( compare_str(buffer, "tachy", lng))478 else if (streq(buffer, "tachy")) 447 479 tests_tachy(); 448 else if ( compare_str(buffer, "radar", lng))480 else if (streq(buffer, "radar")) 449 481 tests_radar(); 450 else if ( compare_str(buffer, "bt", lng))482 else if (streq(buffer, "bt")) 451 483 tests_bt(); 452 else if ( compare_str(buffer, "bt2", lng))484 else if (streq(buffer, "bt2")) 453 485 tests_bt2(); 454 else if ( compare_str(buffer, "all", lng))486 else if (streq(buffer, "all")) 455 487 tests_all(); 456 else if ( compare_str(buffer, "halt", lng))488 else if (streq(buffer, "halt")) 457 489 return 2; 458 else if ( compare_str(buffer, "Al", lng))490 else if (streq(buffer, "Al")) 459 491 nx_motors_rotate_angle(0, 90, 100, 1); 460 else if ( compare_str(buffer, "Ar", lng))492 else if (streq(buffer, "Ar")) 461 493 nx_motors_rotate_angle(0, -90, 100, 1); 462 else if ( compare_str(buffer, "Ac", lng)) {494 else if (streq(buffer, "Ac")) { 463 495 nx_motors_rotate(0, 75); 464 496 while((t = nx_motors_get_tach_count(0)) != 0) { … … 473 505 } 474 506 nx_motors_stop(0, 1); 475 } else if ( compare_str(buffer, "BCf", lng)) {507 } else if (streq(buffer, "BCf")) { 476 508 nx_motors_rotate(1, -100); 477 509 nx_motors_rotate(2, -100); … … 479 511 nx_motors_stop(1, 0); 480 512 nx_motors_stop(2, 0); 481 } else if ( compare_str(buffer, "BCr", lng)) {513 } else if (streq(buffer, "BCr")) { 482 514 nx_motors_rotate(1, 80); 483 515 nx_motors_rotate(2, 80); … … 588 620 /* Start interpreting */ 589 621 590 i = tests_command(buffer , lng);622 i = tests_command(buffer); 591 623 592 624 nx_bt_stream_read((U8 *)&buffer, 2); … … 664 696 /* Start interpreting */ 665 697 666 i = tests_command(buffer , lng);698 i = tests_command(buffer); 667 699 668 700 if (i == 2) { … … 701 733 702 734 if ( (lng = nx_usb_data_read()) > 0) { 703 if ( compare_str(buffer, "halt", lng)) {735 if (streq(buffer, "halt")) { 704 736 break; 705 737 } … … 769 801 test_silent = TRUE; 770 802 803 tests_util(); 771 804 tests_display(); 772 805 tests_sound(); -
nxos/systems/tests/tests.h
r489 r556 2 2 #define __NXOS_TESTS_H__ 3 3 4 void tests_util(void); 4 5 void tests_motor(void); 5 6 void tests_sound(void);
