test_common.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Common functions used by tests.
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "test_common.h"
  11. /* Global test success flag. */
  12. int gTestSuccess = 1;
  13. int test_eq(int result, int expected,
  14. const char *preamble, const char *desc, const char *comment)
  15. {
  16. if (result == expected) {
  17. fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP,
  18. preamble, comment ? comment : desc);
  19. return 1;
  20. } else {
  21. fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP,
  22. preamble, comment ? comment : desc);
  23. fprintf(stderr, " Expected: 0x%x (%d), got: 0x%x (%d)\n",
  24. expected, expected, result, result);
  25. gTestSuccess = 0;
  26. return 0;
  27. }
  28. }
  29. int test_neq(int result, int not_expected,
  30. const char *preamble, const char *desc, const char *comment)
  31. {
  32. if (result != not_expected) {
  33. fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
  34. preamble, desc, comment);
  35. return 1;
  36. } else {
  37. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  38. preamble, desc, comment);
  39. fprintf(stderr, " Didn't expect 0x%x (%d), but got it.\n",
  40. not_expected, not_expected);
  41. gTestSuccess = 0;
  42. return 0;
  43. }
  44. }
  45. int test_ptr_eq(const void* result, const void* expected,
  46. const char *preamble, const char *desc, const char *comment)
  47. {
  48. if (result == expected) {
  49. fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
  50. preamble, desc, comment);
  51. return 1;
  52. } else {
  53. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  54. preamble, desc, comment);
  55. fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n", (long)expected,
  56. (long)result);
  57. gTestSuccess = 0;
  58. return 0;
  59. }
  60. }
  61. int test_ptr_neq(const void* result, const void* not_expected,
  62. const char *preamble, const char *desc, const char *comment)
  63. {
  64. if (result != not_expected) {
  65. fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
  66. preamble, desc, comment);
  67. return 1;
  68. } else {
  69. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  70. preamble, desc, comment);
  71. fprintf(stderr, " Didn't expect 0x%lx, but got it\n",
  72. (long)not_expected);
  73. gTestSuccess = 0;
  74. return 0;
  75. }
  76. }
  77. int test_str_eq(const char* result, const char* expected,
  78. const char *preamble, const char *desc, const char *comment)
  79. {
  80. if (!result || !expected) {
  81. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  82. preamble, desc, comment);
  83. fprintf(stderr, " String compare with NULL\n");
  84. gTestSuccess = 0;
  85. return 0;
  86. } else if (!strcmp(result, expected)) {
  87. fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
  88. preamble, desc, comment);
  89. return 1;
  90. } else {
  91. fprintf(stderr, "%s " COL_RED "FAILED\n" COL_STOP, comment);
  92. fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n", expected,
  93. result);
  94. gTestSuccess = 0;
  95. return 0;
  96. }
  97. }
  98. int test_str_neq(const char* result, const char* not_expected,
  99. const char *preamble, const char *desc, const char *comment)
  100. {
  101. if (!result || !not_expected) {
  102. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  103. preamble, desc, comment);
  104. fprintf(stderr, " String compare with NULL\n");
  105. gTestSuccess = 0;
  106. return 0;
  107. } else if (strcmp(result, not_expected)) {
  108. fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
  109. preamble, desc, comment);
  110. return 1;
  111. } else {
  112. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  113. preamble, desc, comment);
  114. fprintf(stderr, " Didn't expect: \"%s\", but got it\n", not_expected);
  115. gTestSuccess = 0;
  116. return 0;
  117. }
  118. }
  119. int test_succ(int result,
  120. const char *preamble, const char *desc, const char *comment)
  121. {
  122. if (result == 0) {
  123. fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP,
  124. preamble, comment ? comment : desc);
  125. } else {
  126. fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP,
  127. preamble, comment ? comment : desc);
  128. fprintf(stderr, " Expected SUCCESS, got: 0x%x (%d)\n", result, result);
  129. gTestSuccess = 0;
  130. }
  131. return !result;
  132. }
  133. int test_true(int result,
  134. const char *preamble, const char *desc, const char *comment)
  135. {
  136. if (result) {
  137. fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
  138. preamble, desc, comment);
  139. } else {
  140. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  141. preamble, desc, comment);
  142. fprintf(stderr, " Expected TRUE, got 0\n");
  143. gTestSuccess = 0;
  144. }
  145. return result;
  146. }
  147. int test_false(int result,
  148. const char *preamble, const char *desc, const char *comment)
  149. {
  150. if (!result) {
  151. fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
  152. preamble, desc, comment);
  153. } else {
  154. fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
  155. preamble, desc, comment);
  156. fprintf(stderr, " Expected FALSE, got: 0x%lx\n", (long)result);
  157. gTestSuccess = 0;
  158. }
  159. return !result;
  160. }