test/testautomation_stdlib.c
changeset 7216 3586fc0ba156
child 7219 c577dfb69153
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testautomation_stdlib.c	Fri May 24 03:23:21 2013 -0700
     1.3 @@ -0,0 +1,142 @@
     1.4 +/**
     1.5 + * Standard C library routine test suite
     1.6 + */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +
    1.10 +#include "SDL.h"
    1.11 +#include "SDL_test.h"
    1.12 +
    1.13 +
    1.14 +/* Test case functions */
    1.15 +
    1.16 +/**
    1.17 + * @brief Call to SDL_strlcpy
    1.18 + */
    1.19 +#undef SDL_strlcpy
    1.20 +int
    1.21 +stdlib_strlcpy(void *arg)
    1.22 +{
    1.23 +  size_t result;
    1.24 +  char text[1024];
    1.25 +  const char *expected;
    1.26 +
    1.27 +  result = SDL_strlcpy(text, "foo", sizeof(text));
    1.28 +  expected = "foo";
    1.29 +  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
    1.30 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.31 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    1.32 +
    1.33 +  result = SDL_strlcpy(text, "foo", 2);
    1.34 +  expected = "f";
    1.35 +  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
    1.36 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.37 +  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
    1.38 +
    1.39 +  return TEST_COMPLETED;
    1.40 +}
    1.41 +
    1.42 +/**
    1.43 + * @brief Call to SDL_snprintf
    1.44 + */
    1.45 +#undef SDL_snprintf
    1.46 +int
    1.47 +stdlib_snprintf(void *arg)
    1.48 +{
    1.49 +  int result;
    1.50 +  char text[1024];
    1.51 +  const char *expected;
    1.52 +
    1.53 +  result = SDL_snprintf(text, sizeof(text), "%s", "foo");
    1.54 +  expected = "foo";
    1.55 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
    1.56 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.57 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    1.58 +
    1.59 +  result = SDL_snprintf(text, 2, "%s", "foo");
    1.60 +  expected = "f";
    1.61 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
    1.62 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.63 +  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
    1.64 +
    1.65 +  result = SDL_snprintf(NULL, 0, "%s", "foo");
    1.66 +  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
    1.67 +
    1.68 +  result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
    1.69 +  expected = "1.000000";
    1.70 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
    1.71 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.72 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    1.73 +
    1.74 +  result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
    1.75 +  expected = "1";
    1.76 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
    1.77 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.78 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    1.79 +
    1.80 +  result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
    1.81 +  expected = "1.";
    1.82 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
    1.83 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.84 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    1.85 +
    1.86 +  result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
    1.87 +  expected = "1.333333";
    1.88 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
    1.89 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.90 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    1.91 +
    1.92 +  result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
    1.93 +  expected = "+1.333333";
    1.94 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
    1.95 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
    1.96 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    1.97 +
    1.98 +  result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
    1.99 +  expected = "1.33";
   1.100 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
   1.101 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
   1.102 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
   1.103 +
   1.104 +  result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
   1.105 +  expected = "  1.33";
   1.106 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
   1.107 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
   1.108 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
   1.109 +
   1.110 +  result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
   1.111 +  expected = "001.33";
   1.112 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
   1.113 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
   1.114 +  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
   1.115 +
   1.116 +  result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
   1.117 +  expected = "001.";
   1.118 +  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
   1.119 +  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
   1.120 +  SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
   1.121 +
   1.122 +  return TEST_COMPLETED;
   1.123 +}
   1.124 +
   1.125 +/* ================= Test References ================== */
   1.126 +
   1.127 +/* Standard C routine test cases */
   1.128 +static const SDLTest_TestCaseReference stdlibTest1 =
   1.129 +        { (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED };
   1.130 +
   1.131 +static const SDLTest_TestCaseReference stdlibTest2 =
   1.132 +        { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_sprintf", TEST_ENABLED };
   1.133 +
   1.134 +/* Sequence of Standard C routine test cases */
   1.135 +static const SDLTest_TestCaseReference *stdlibTests[] =  {
   1.136 +    &stdlibTest1, &stdlibTest2, NULL
   1.137 +};
   1.138 +
   1.139 +/* Timer test suite (global) */
   1.140 +SDLTest_TestSuiteReference stdlibTestSuite = {
   1.141 +    "Standard C routines",
   1.142 +    NULL,
   1.143 +    stdlibTests,
   1.144 +    NULL
   1.145 +};