cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

testautomation_stdlib.c (10929B)


      1/**
      2 * Standard C library routine test suite
      3 */
      4
      5#include <stdio.h>
      6
      7#include "SDL.h"
      8#include "SDL_test.h"
      9
     10
     11/* Test case functions */
     12
     13/**
     14 * @brief Call to SDL_strlcpy
     15 */
     16#undef SDL_strlcpy
     17int
     18stdlib_strlcpy(void *arg)
     19{
     20  size_t result;
     21  char text[1024];
     22  const char *expected;
     23
     24  result = SDL_strlcpy(text, "foo", sizeof(text));
     25  expected = "foo";
     26  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\")");
     27  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     28  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
     29
     30  result = SDL_strlcpy(text, "foo", 2);
     31  expected = "f";
     32  SDLTest_AssertPass("Call to SDL_strlcpy(\"foo\") with buffer size 2");
     33  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     34  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
     35
     36  return TEST_COMPLETED;
     37}
     38
     39/**
     40 * @brief Call to SDL_snprintf
     41 */
     42#undef SDL_snprintf
     43int
     44stdlib_snprintf(void *arg)
     45{
     46  int result;
     47  char text[1024];
     48  const char *expected;
     49
     50  result = SDL_snprintf(text, sizeof(text), "%s", "foo");
     51  expected = "foo";
     52  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\")");
     53  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     54  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
     55
     56  result = SDL_snprintf(text, 2, "%s", "foo");
     57  expected = "f";
     58  SDLTest_AssertPass("Call to SDL_snprintf(\"%%s\", \"foo\") with buffer size 2");
     59  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     60  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
     61
     62  result = SDL_snprintf(NULL, 0, "%s", "foo");
     63  SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
     64
     65  result = SDL_snprintf(text, sizeof(text), "%f", 1.0);
     66  expected = "1.000000";
     67  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0)");
     68  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     69  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
     70
     71  result = SDL_snprintf(text, sizeof(text), "%.f", 1.0);
     72  expected = "1";
     73  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.f\", 1.0)");
     74  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     75  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
     76
     77  result = SDL_snprintf(text, sizeof(text), "%#.f", 1.0);
     78  expected = "1.";
     79  SDLTest_AssertPass("Call to SDL_snprintf(\"%%#.f\", 1.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
     83  result = SDL_snprintf(text, sizeof(text), "%f", 1.0 + 1.0 / 3.0);
     84  expected = "1.333333";
     85  SDLTest_AssertPass("Call to SDL_snprintf(\"%%f\", 1.0 + 1.0 / 3.0)");
     86  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     87  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
     88
     89  result = SDL_snprintf(text, sizeof(text), "%+f", 1.0 + 1.0 / 3.0);
     90  expected = "+1.333333";
     91  SDLTest_AssertPass("Call to SDL_snprintf(\"%%+f\", 1.0 + 1.0 / 3.0)");
     92  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     93  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
     94
     95  result = SDL_snprintf(text, sizeof(text), "%.2f", 1.0 + 1.0 / 3.0);
     96  expected = "1.33";
     97  SDLTest_AssertPass("Call to SDL_snprintf(\"%%.2f\", 1.0 + 1.0 / 3.0)");
     98  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: %s, got: %s", expected, text);
     99  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    100
    101  result = SDL_snprintf(text, sizeof(text), "%6.2f", 1.0 + 1.0 / 3.0);
    102  expected = "  1.33";
    103  SDLTest_AssertPass("Call to SDL_snprintf(\"%%6.2f\", 1.0 + 1.0 / 3.0)");
    104  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
    105  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    106
    107  result = SDL_snprintf(text, sizeof(text), "%06.2f", 1.0 + 1.0 / 3.0);
    108  expected = "001.33";
    109  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0)");
    110  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
    111  SDLTest_AssertCheck(result == SDL_strlen(text), "Check result value, expected: %d, got: %d", SDL_strlen(text), result);
    112
    113  result = SDL_snprintf(text, 5, "%06.2f", 1.0 + 1.0 / 3.0);
    114  expected = "001.";
    115  SDLTest_AssertPass("Call to SDL_snprintf(\"%%06.2f\", 1.0 + 1.0 / 3.0) with buffer size 5");
    116  SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
    117  SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
    118
    119  return TEST_COMPLETED;
    120}
    121
    122/**
    123 * @brief Call to SDL_getenv and SDL_setenv
    124 */
    125int
    126stdlib_getsetenv(void *arg)
    127{
    128  const int nameLen = 16;
    129  char name[17];
    130  int counter;
    131  int result;
    132  char * value1;
    133  char * value2;
    134  char * expected;
    135  int overwrite;
    136  char * text;
    137
    138  /* Create a random name. This tests SDL_getenv, since we need to */
    139  /* make sure the variable is not set yet (it shouldn't). */
    140  do {
    141    for(counter = 0; counter < nameLen; counter++) {
    142      name[counter] = (char)SDLTest_RandomIntegerInRange(65, 90);
    143    }
    144    name[nameLen] = '\0';
    145    
    146    text = SDL_getenv(name);
    147    SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
    148    if (text != NULL) {
    149      SDLTest_Log("Expected: NULL, Got: '%s' (%i)", text, SDL_strlen(text));
    150    }
    151  } while (text != NULL);
    152   
    153  /* Create random values to set */                    
    154  value1 = SDLTest_RandomAsciiStringOfSize(10);
    155  value2 = SDLTest_RandomAsciiStringOfSize(10);
    156
    157  /* Set value 1 without overwrite */
    158  overwrite = 0;
    159  expected = value1;
    160  result = SDL_setenv(name, value1, overwrite);
    161  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
    162  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
    163
    164  /* Check value */
    165  text = SDL_getenv(name);
    166  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
    167  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
    168  if (text != NULL) {
    169    SDLTest_AssertCheck(
    170      SDL_strcmp(text, expected) == 0, 
    171      "Verify returned text, expected: %s, got: %s",
    172      expected,
    173      text);
    174  }
    175  
    176  /* Set value 2 with overwrite */
    177  overwrite = 1;
    178  expected = value2;    
    179  result = SDL_setenv(name, value2, overwrite);
    180  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value2, overwrite);
    181  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
    182
    183  /* Check value */
    184  text = SDL_getenv(name);
    185  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
    186  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
    187  if (text != NULL) {
    188    SDLTest_AssertCheck(
    189      SDL_strcmp(text, expected) == 0, 
    190      "Verify returned text, expected: %s, got: %s",
    191      expected,
    192      text);
    193  }
    194
    195  /* Set value 1 without overwrite */
    196  overwrite = 0;
    197  expected = value2;    
    198  result = SDL_setenv(name, value1, overwrite);
    199  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
    200  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
    201
    202  /* Check value */
    203  text = SDL_getenv(name);
    204  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
    205  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
    206  if (text != NULL) {
    207    SDLTest_AssertCheck(
    208      SDL_strcmp(text, expected) == 0, 
    209      "Verify returned text, expected: %s, got: %s",
    210      expected,
    211      text);
    212  }
    213  
    214  /* Set value 1 without overwrite */
    215  overwrite = 1;
    216  expected = value1;
    217  result = SDL_setenv(name, value1, overwrite);
    218  SDLTest_AssertPass("Call to SDL_setenv('%s','%s', %i)", name, value1, overwrite);
    219  SDLTest_AssertCheck(result == 0, "Check result, expected: 0, got: %i", result);
    220
    221  /* Check value */
    222  text = SDL_getenv(name);
    223  SDLTest_AssertPass("Call to SDL_getenv('%s')", name);
    224  SDLTest_AssertCheck(text != NULL, "Verify returned text is not NULL");
    225  if (text != NULL) {
    226    SDLTest_AssertCheck(
    227      SDL_strcmp(text, expected) == 0, 
    228      "Verify returned text, expected: %s, got: %s",
    229      expected,
    230      text);
    231  }
    232
    233  /* Negative cases */
    234  for (overwrite=0; overwrite <= 1; overwrite++) { 
    235    result = SDL_setenv(NULL, value1, overwrite);
    236    SDLTest_AssertPass("Call to SDL_setenv(NULL,'%s', %i)", value1, overwrite);
    237    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
    238    result = SDL_setenv("", value1, overwrite);
    239    SDLTest_AssertPass("Call to SDL_setenv('','%s', %i)", value1, overwrite);
    240    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
    241    result = SDL_setenv("=", value1, overwrite);
    242    SDLTest_AssertPass("Call to SDL_setenv('=','%s', %i)", value1, overwrite);
    243    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
    244    result = SDL_setenv(name, NULL, overwrite);
    245    SDLTest_AssertPass("Call to SDL_setenv('%s', NULL, %i)", name, overwrite);
    246    SDLTest_AssertCheck(result == -1, "Check result, expected: -1, got: %i", result);
    247  }
    248
    249  /* Clean up */
    250  SDL_free(value1);
    251  SDL_free(value2);
    252    
    253  return TEST_COMPLETED;
    254}
    255
    256/* ================= Test References ================== */
    257
    258/* Standard C routine test cases */
    259static const SDLTest_TestCaseReference stdlibTest1 =
    260        { (SDLTest_TestCaseFp)stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED };
    261
    262static const SDLTest_TestCaseReference stdlibTest2 =
    263        { (SDLTest_TestCaseFp)stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED };
    264
    265static const SDLTest_TestCaseReference stdlibTest3 =
    266        { (SDLTest_TestCaseFp)stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED };
    267
    268/* Sequence of Standard C routine test cases */
    269static const SDLTest_TestCaseReference *stdlibTests[] =  {
    270    &stdlibTest1, &stdlibTest2, &stdlibTest3, NULL
    271};
    272
    273/* Timer test suite (global) */
    274SDLTest_TestSuiteReference stdlibTestSuite = {
    275    "Stdlib",
    276    NULL,
    277    stdlibTests,
    278    NULL
    279};