testautomation_stdlib.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Standard C library routine test suite
  3. */
  4. #include <stdio.h>
  5. #include "SDL.h"
  6. #include "SDL_test.h"
  7. /* Test case functions */
  8. /**
  9. * @brief Call to SDL_strlcpy
  10. */
  11. #undef SDL_strlcpy
  12. int
  13. stdlib_strlcpy(void *arg)
  14. {
  15. size_t result;
  16. char text[1024];
  17. const char *expected;
  18. result = SDL_strlcpy(text, "foo", sizeof(text));
  19. expected = "foo";
  20. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
  21. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  22. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  23. result = SDL_strlcpy(text, "foo", 2);
  24. expected = "f";
  25. SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
  26. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  27. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  28. return TEST_COMPLETED;
  29. }
  30. /**
  31. * @brief Call to SDL_snprintf
  32. */
  33. #undef SDL_snprintf
  34. int
  35. stdlib_snprintf(void *arg)
  36. {
  37. int result;
  38. char text[1024];
  39. const char *expected;
  40. result = SDL_snprintf(text, sizeof(text), "%s", "foo");
  41. expected = "foo";
  42. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
  43. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  44. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  45. result = SDL_snprintf(text, 2, "%s", "foo");
  46. expected = "f";
  47. SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
  48. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  49. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  50. result = SDL_snprintf(NULL, 0, "%s", "foo");
  51. SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
  52. result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
  53. expected = "1.000000";
  54. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
  55. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  56. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  57. result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
  58. expected = "1";
  59. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
  60. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  61. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  62. result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
  63. expected = "1.";
  64. SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
  65. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  66. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  67. result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
  68. expected = "1.333333";
  69. SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
  70. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  71. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  72. result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
  73. expected = "+1.333333";
  74. SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
  75. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  76. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  77. result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
  78. expected = "1.33";
  79. SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
  80. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
  81. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  82. result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
  83. expected = " 1.33";
  84. SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
  85. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  86. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  87. result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
  88. expected = "001.33";
  89. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
  90. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  91. SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
  92. result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
  93. expected = "001.";
  94. SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
  95. SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
  96. SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
  97. return TEST_COMPLETED;
  98. }
  99. /* ================= Test References ================== */
  100. /* Standard C routine test cases */
  101. static const SDLTest_TestCaseReference stdlibTest1 =
  102. { (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED };
  103. static const SDLTest_TestCaseReference stdlibTest2 =
  104. { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED };
  105. /* Sequence of Standard C routine test cases */
  106. static const SDLTest_TestCaseReference *stdlibTests[] = {
  107. &stdlibTest1, &stdlibTest2, NULL
  108. };
  109. /* Timer test suite (global) */
  110. SDLTest_TestSuiteReference stdlibTestSuite = {
  111. "Standard C routines",
  112. NULL,
  113. stdlibTests,
  114. NULL
  115. };