test/testautomation_stdlib.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 10782 1de6d4633ee6
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
slouken@7216
     1
/**
slouken@7216
     2
 * Standard C library routine test suite
slouken@7216
     3
 */
slouken@7216
     4
slouken@7216
     5
#include <stdio.h>
slouken@7216
     6
slouken@7216
     7
#include "SDL.h"
slouken@7216
     8
#include "SDL_test.h"
slouken@7216
     9
slouken@7216
    10
slouken@7216
    11
/* Test case functions */
slouken@7216
    12
slouken@7216
    13
/**
slouken@7216
    14
 * @brief Call to SDL_strlcpy
slouken@7216
    15
 */
slouken@7216
    16
#undef SDL_strlcpy
slouken@7216
    17
int
slouken@7216
    18
stdlib_strlcpy(void *arg)
slouken@7216
    19
{
slouken@7216
    20
  size_t result;
slouken@7216
    21
  char text[1024];
slouken@7216
    22
  const char *expected;
slouken@7216
    23
slouken@7216
    24
  result = SDL_strlcpy(text, "foo", sizeof(text));
slouken@7216
    25
  expected = "foo";
slouken@7216
    26
  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
slouken@7216
    27
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    28
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), (int) result);
slouken@7216
    29
slouken@7216
    30
  result = SDL_strlcpy(text, "foo", 2);
slouken@7216
    31
  expected = "f";
slouken@7216
    32
  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
slouken@7216
    33
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    34
  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int) result);
slouken@7216
    35
slouken@7216
    36
  return TEST_COMPLETED;
slouken@7216
    37
}
slouken@7216
    38
slouken@7216
    39
/**
slouken@7216
    40
 * @brief Call to SDL_snprintf
slouken@7216
    41
 */
slouken@7216
    42
#undef SDL_snprintf
slouken@7216
    43
int
slouken@7216
    44
stdlib_snprintf(void *arg)
slouken@7216
    45
{
slouken@7216
    46
  int result;
slouken@7216
    47
  char text[1024];
slouken@7216
    48
  const char *expected;
slouken@7216
    49
slouken@7216
    50
  result = SDL_snprintf(text, sizeof(text), "%s", "foo");
slouken@7216
    51
  expected = "foo";
slouken@7216
    52
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
slouken@7216
    53
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    54
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
    55
slouken@7216
    56
  result = SDL_snprintf(text, 2, "%s", "foo");
slouken@7216
    57
  expected = "f";
slouken@7216
    58
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
slouken@7216
    59
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
slouken@7216
    60
  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
slouken@7216
    61
slouken@7216
    62
  result = SDL_snprintf(NULL, 0, "%s", "foo");
slouken@7216
    63
  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
slouken@7216
    64
slouken@7216
    65
  result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
slouken@7216
    66
  expected = "1.000000";
slouken@7216
    67
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
slouken@7216
    68
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    69
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
    70
slouken@7216
    71
  result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
slouken@7216
    72
  expected = "1";
slouken@7216
    73
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
slouken@7216
    74
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    75
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
    76
slouken@7216
    77
  result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
slouken@7216
    78
  expected = "1.";
slouken@7216
    79
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.0)");
slouken@7216
    80
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    81
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
    82
slouken@7216
    83
  result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
slouken@7216
    84
  expected = "1.333333";
slouken@7216
    85
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
slouken@7216
    86
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    87
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
    88
slouken@7216
    89
  result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
slouken@7216
    90
  expected = "+1.333333";
slouken@7216
    91
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
slouken@7216
    92
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    93
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
    94
slouken@7216
    95
  result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
slouken@7216
    96
  expected = "1.33";
slouken@7216
    97
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
slouken@7216
    98
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
icculus@10782
    99
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
   100
slouken@7216
   101
  result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
slouken@7216
   102
  expected = "  1.33";
slouken@7216
   103
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
slouken@7216
   104
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
icculus@10782
   105
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
   106
slouken@7216
   107
  result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
slouken@7216
   108
  expected = "001.33";
slouken@7216
   109
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
slouken@7216
   110
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
icculus@10782
   111
  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", (int) SDL_strlen(text), result);
slouken@7216
   112
slouken@7216
   113
  result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
slouken@7216
   114
  expected = "001.";
slouken@7216
   115
  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
slouken@7216
   116
  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
slouken@7216
   117
  SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
slouken@7216
   118
slouken@7216
   119
  return TEST_COMPLETED;
slouken@7216
   120
}
slouken@7216
   121
aschiffler@8645
   122
/**
aschiffler@8645
   123
 * @brief Call to SDL_getenv and SDL_setenv
aschiffler@8645
   124
 */
aschiffler@8645
   125
int
aschiffler@8645
   126
stdlib_getsetenv(void *arg)
aschiffler@8645
   127
{
aschiffler@8645
   128
  const int nameLen = 16;
aschiffler@8646
   129
  char name[17];
aschiffler@8645
   130
  int counter;
aschiffler@8645
   131
  int result;
aschiffler@8645
   132
  char * value1;
aschiffler@8645
   133
  char * value2;
aschiffler@8645
   134
  char * expected;
aschiffler@8645
   135
  int overwrite;
aschiffler@8645
   136
  char * text;
aschiffler@8645
   137
aschiffler@8645
   138
  /* Create a random name. This tests SDL_getenv, since we need to */
aschiffler@8645
   139
  /* make sure the variable is not set yet (it shouldn't). */
aschiffler@8645
   140
  do {
aschiffler@8645
   141
    for(counter = 0; counter < nameLen; counter++) {
aschiffler@8645
   142
      name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
aschiffler@8645
   143
    }
aschiffler@8645
   144
    name[nameLen] = '\0';
aschiffler@8645
   145
    
aschiffler@8645
   146
    text = SDL_getenv(name);
aschiffler@8645
   147
    SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   148
    if (text != NULL) {
icculus@10782
   149
      SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, (int) SDL_strlen(text));
aschiffler@8645
   150
    }
aschiffler@8645
   151
  } while (text != NULL);
aschiffler@8645
   152
   
aschiffler@8645
   153
  /* Create random values to set */                    
aschiffler@8645
   154
  value1 = SDLTest_RandomAsciiStringOfSize(10);
aschiffler@8645
   155
  value2 = SDLTest_RandomAsciiStringOfSize(10);
aschiffler@8645
   156
aschiffler@8645
   157
  /* Set value 1 without overwrite */
aschiffler@8645
   158
  overwrite = 0;
aschiffler@8645
   159
  expected = value1;
aschiffler@8645
   160
  result = SDL_setenv(name, value1, overwrite);
aschiffler@8645
   161
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
aschiffler@8645
   162
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   163
aschiffler@8645
   164
  /* Check value */
aschiffler@8645
   165
  text = SDL_getenv(name);
aschiffler@8645
   166
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   167
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   168
  if (text != NULL) {
aschiffler@8645
   169
    SDLTest_AssertCheck(
aschiffler@8645
   170
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   171
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   172
      expected,
aschiffler@8645
   173
      text);
aschiffler@8645
   174
  }
aschiffler@8645
   175
  
aschiffler@8645
   176
  /* Set value 2 with overwrite */
aschiffler@8645
   177
  overwrite = 1;
aschiffler@8645
   178
  expected = value2;    
aschiffler@8645
   179
  result = SDL_setenv(name, value2, overwrite);
aschiffler@8645
   180
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
aschiffler@8645
   181
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   182
aschiffler@8645
   183
  /* Check value */
aschiffler@8645
   184
  text = SDL_getenv(name);
aschiffler@8645
   185
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   186
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   187
  if (text != NULL) {
aschiffler@8645
   188
    SDLTest_AssertCheck(
aschiffler@8645
   189
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   190
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   191
      expected,
aschiffler@8645
   192
      text);
aschiffler@8645
   193
  }
aschiffler@8645
   194
aschiffler@8645
   195
  /* Set value 1 without overwrite */
aschiffler@8645
   196
  overwrite = 0;
aschiffler@8645
   197
  expected = value2;    
aschiffler@8645
   198
  result = SDL_setenv(name, value1, overwrite);
aschiffler@8645
   199
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
aschiffler@8645
   200
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   201
aschiffler@8645
   202
  /* Check value */
aschiffler@8645
   203
  text = SDL_getenv(name);
aschiffler@8645
   204
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   205
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   206
  if (text != NULL) {
aschiffler@8645
   207
    SDLTest_AssertCheck(
aschiffler@8645
   208
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   209
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   210
      expected,
aschiffler@8645
   211
      text);
aschiffler@8645
   212
  }
aschiffler@8645
   213
  
aschiffler@8645
   214
  /* Set value 1 without overwrite */
aschiffler@8645
   215
  overwrite = 1;
aschiffler@8645
   216
  expected = value1;
aschiffler@8645
   217
  result = SDL_setenv(name, value1, overwrite);
aschiffler@8645
   218
  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
aschiffler@8645
   219
  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
aschiffler@8645
   220
aschiffler@8645
   221
  /* Check value */
aschiffler@8645
   222
  text = SDL_getenv(name);
aschiffler@8645
   223
  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
aschiffler@8645
   224
  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
aschiffler@8645
   225
  if (text != NULL) {
aschiffler@8645
   226
    SDLTest_AssertCheck(
aschiffler@8645
   227
      SDL_strcmp(text, expected) == 0, 
aschiffler@8645
   228
      "Verify returned text, expected: %s, got: %s",
aschiffler@8645
   229
      expected,
aschiffler@8645
   230
      text);
aschiffler@8645
   231
  }
aschiffler@8645
   232
aschiffler@8645
   233
  /* Negative cases */
aschiffler@8645
   234
  for (overwrite=0; overwrite <= 1; overwrite++) { 
aschiffler@8645
   235
    result = SDL_setenv(NULL, value1, overwrite);
aschiffler@8645
   236
    SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
aschiffler@8645
   237
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   238
    result = SDL_setenv("", value1, overwrite);
aschiffler@8645
   239
    SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
aschiffler@8645
   240
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   241
    result = SDL_setenv("=", value1, overwrite);
aschiffler@8645
   242
    SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
aschiffler@8645
   243
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   244
    result = SDL_setenv(name, NULL, overwrite);
aschiffler@8645
   245
    SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
aschiffler@8645
   246
    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
aschiffler@8645
   247
  }
aschiffler@8645
   248
aschiffler@8645
   249
  /* Clean up */
aschiffler@8645
   250
  SDL_free(value1);
aschiffler@8645
   251
  SDL_free(value2);
aschiffler@8645
   252
    
aschiffler@8645
   253
  return TEST_COMPLETED;
aschiffler@8645
   254
}
aschiffler@8645
   255
philipp@10188
   256
/**
philipp@10188
   257
 * @brief Call to SDL_sscanf
philipp@10188
   258
 */
philipp@10188
   259
#undef SDL_sscanf
philipp@10188
   260
int
philipp@10188
   261
stdlib_sscanf(void *arg)
philipp@10188
   262
{
philipp@10188
   263
  int output;
philipp@10188
   264
  int result;
philipp@10188
   265
  int expected_output;
philipp@10188
   266
  int expected_result;
philipp@10188
   267
philipp@10188
   268
  expected_output = output = 123;
philipp@10188
   269
  expected_result = -1;
philipp@10188
   270
  result = SDL_sscanf("", "%i", &output);
philipp@10188
   271
  SDLTest_AssertPass("Call to SDL_sscanf(\"\", \"%%i\", &output)");
philipp@10188
   272
  SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
philipp@10188
   273
  SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
philipp@10188
   274
philipp@10188
   275
  expected_output = output = 123;
philipp@10188
   276
  expected_result = 0;
philipp@10188
   277
  result = SDL_sscanf("a", "%i", &output);
philipp@10188
   278
  SDLTest_AssertPass("Call to SDL_sscanf(\"a\", \"%%i\", &output)");
philipp@10188
   279
  SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
philipp@10188
   280
  SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
philipp@10188
   281
philipp@10188
   282
  output = 123;
philipp@10188
   283
  expected_output = 2;
philipp@10188
   284
  expected_result = 1;
philipp@10188
   285
  result = SDL_sscanf("2", "%i", &output);
philipp@10188
   286
  SDLTest_AssertPass("Call to SDL_sscanf(\"2\", \"%%i\", &output)");
philipp@10188
   287
  SDLTest_AssertCheck(expected_output == output, "Check output, expected: %i, got: %i", expected_output, output);
philipp@10188
   288
  SDLTest_AssertCheck(expected_result == result, "Check return value, expected: %i, got: %i", expected_result, result);
philipp@10188
   289
philipp@10188
   290
  return TEST_COMPLETED;
philipp@10188
   291
}
philipp@10188
   292
slouken@7216
   293
/* ================= Test References ================== */
slouken@7216
   294
slouken@7216
   295
/* Standard C routine test cases */
slouken@7216
   296
static const SDLTest_TestCaseReference stdlibTest1 =
slouken@7216
   297
        { (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED };
slouken@7216
   298
slouken@7216
   299
static const SDLTest_TestCaseReference stdlibTest2 =
philipp@7219
   300
        { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED };
slouken@7216
   301
aschiffler@8645
   302
static const SDLTest_TestCaseReference stdlibTest3 =
aschiffler@8645
   303
        { (SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED };
aschiffler@8645
   304
philipp@10188
   305
static const SDLTest_TestCaseReference stdlibTest4 =
philipp@10188
   306
        { (SDLTest_TestCaseFp)stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED };
philipp@10188
   307
slouken@7216
   308
/* Sequence of Standard C routine test cases */
slouken@7216
   309
static const SDLTest_TestCaseReference *stdlibTests[] =  {
philipp@10188
   310
    &stdlibTest1, &stdlibTest2, &stdlibTest3, &stdlibTest4, NULL
slouken@7216
   311
};
slouken@7216
   312
philipp@10189
   313
/* Standard C routine test suite (global) */
slouken@7216
   314
SDLTest_TestSuiteReference stdlibTestSuite = {
aschiffler@8645
   315
    "Stdlib",
slouken@7216
   316
    NULL,
slouken@7216
   317
    stdlibTests,
slouken@7216
   318
    NULL
slouken@7216
   319
};