test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "common/test.h"
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <stdarg.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. void fail(const char *fmt, ...)
  30. {
  31. va_list ap;
  32. va_start(ap, fmt);
  33. vfprintf(stderr, fmt, ap);
  34. va_end(ap);
  35. exit(1);
  36. }
  37. static int vexpect_decode(const char *expected, const char *text, int ty,
  38. const char *str)
  39. {
  40. switch (ty) {
  41. case TEST_ERROR_EQ:
  42. if (strcmp(expected, str) != 0) {
  43. fprintf(stderr, "error: expected '%s', but got '%s', which "
  44. "is not equal. %s\n", expected, str, text);
  45. return -1;
  46. }
  47. break;
  48. case TEST_ERROR_GE:
  49. if (strcmp(expected, str) > 0) {
  50. fprintf(stderr, "error: expected '%s', but got '%s'. "
  51. "Expected something greater or equal. %s\n",
  52. expected, str, text);
  53. return -1;
  54. }
  55. break;
  56. case TEST_ERROR_GT:
  57. if (strcmp(expected, str) >= 0) {
  58. fprintf(stderr, "error: expected '%s', but got '%s'. "
  59. "Expected something greater. %s\n",
  60. expected, str, text);
  61. return -1;
  62. }
  63. break;
  64. case TEST_ERROR_LE:
  65. if (strcmp(expected, str) < 0) {
  66. fprintf(stderr, "error: expected '%s', but got '%s'. "
  67. "Expected something less or equal. %s\n",
  68. expected, str, text);
  69. return -1;
  70. }
  71. break;
  72. case TEST_ERROR_LT:
  73. if (strcmp(expected, str) <= 0) {
  74. fprintf(stderr, "error: expected '%s', but got '%s'. "
  75. "Expected something less. %s\n",
  76. expected, str, text);
  77. return -1;
  78. }
  79. break;
  80. case TEST_ERROR_NE:
  81. if (strcmp(expected, str) == 0) {
  82. fprintf(stderr, "error: did not expect '%s': '%s'\n",
  83. expected, text);
  84. return -1;
  85. }
  86. break;
  87. default:
  88. fprintf(stderr, "runtime error: invalid type %d passed in: '%s'\n",
  89. ty, text);
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. int vexpect(const char *expected, const char *text, int ty,
  95. const char *fmt, va_list ap)
  96. {
  97. char *str = NULL;
  98. int ret;
  99. if (vasprintf(&str, fmt, ap) < 0) { // TODO: portability
  100. fprintf(stderr, "error: vasprintf failed: %s\n", text);
  101. return -1;
  102. }
  103. ret = vexpect_decode(expected, text, ty, str);
  104. free(str);
  105. return ret;
  106. }
  107. int expect(const char *expected, const char *text, int ty,
  108. const char *fmt, ...)
  109. {
  110. int ret;
  111. va_list ap;
  112. va_start(ap, fmt);
  113. ret = vexpect(expected, text, ty, fmt, ap);
  114. va_end(ap);
  115. return ret;
  116. }
  117. void *xcalloc(size_t len)
  118. {
  119. void *v = calloc(1, len);
  120. if (!v) {
  121. abort();
  122. }
  123. return v;
  124. }
  125. // vim: ts=4:sw=4:tw=79:et