test/testfile.c
changeset 7639 9406b7dd2f2d
parent 7538 1d187795e103
child 7663 53fe1b64eb2d
equal deleted inserted replaced
7638:783d1cff9b20 7639:9406b7dd2f2d
    42 #endif
    42 #endif
    43 
    43 
    44 static void
    44 static void
    45 cleanup(void)
    45 cleanup(void)
    46 {
    46 {
    47 
       
    48     unlink(FBASENAME1);
    47     unlink(FBASENAME1);
    49     unlink(FBASENAME2);
    48     unlink(FBASENAME2);
    50 }
    49 }
    51 
    50 
    52 static void
    51 static void
    53 rwops_error_quit(unsigned line, SDL_RWops * rwops)
    52 rwops_error_quit(unsigned line, SDL_RWops * rwops)
    54 {
    53 {
    55 
    54     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
    56     printf("testfile.c(%d): failed\n", line);
       
    57     if (rwops) {
    55     if (rwops) {
    58         rwops->close(rwops);    /* This calls SDL_FreeRW(rwops); */
    56         rwops->close(rwops);    /* This calls SDL_FreeRW(rwops); */
    59     }
    57     }
    60     cleanup();
    58     cleanup();
    61     exit(1);                    /* quit with rwops error (test failed) */
    59     exit(1);                    /* quit with rwops error (test failed) */
    69 main(int argc, char *argv[])
    67 main(int argc, char *argv[])
    70 {
    68 {
    71     SDL_RWops *rwops = NULL;
    69     SDL_RWops *rwops = NULL;
    72     char test_buf[30];
    70     char test_buf[30];
    73 
    71 
       
    72     /* Enable standard application logging */
       
    73 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
       
    74 
    74     cleanup();
    75     cleanup();
    75 
    76 
    76 /* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
    77 /* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
    77 
    78 
    78     rwops = SDL_RWFromFile(NULL, NULL);
    79     rwops = SDL_RWFromFile(NULL, NULL);
    88     if (rwops)
    89     if (rwops)
    89         RWOP_ERR_QUIT(rwops);
    90         RWOP_ERR_QUIT(rwops);
    90     rwops = SDL_RWFromFile("something", NULL);
    91     rwops = SDL_RWFromFile("something", NULL);
    91     if (rwops)
    92     if (rwops)
    92         RWOP_ERR_QUIT(rwops);
    93         RWOP_ERR_QUIT(rwops);
    93     printf("test1 OK\n");
    94     SDL_Log("test1 OK\n");
    94 
    95 
    95 /* test 2 : check that inexistent file is not successfully opened/created when required */
    96 /* test 2 : check that inexistent file is not successfully opened/created when required */
    96 /* modes : r, r+ imply that file MUST exist
    97 /* modes : r, r+ imply that file MUST exist
    97    modes : a, a+, w, w+ checks that it succeeds (file may not exists)
    98    modes : a, a+, w, w+ checks that it succeeds (file may not exists)
    98 
    99 
   121     rwops = SDL_RWFromFile(FBASENAME2, "ab+");
   122     rwops = SDL_RWFromFile(FBASENAME2, "ab+");
   122     if (!rwops)
   123     if (!rwops)
   123         RWOP_ERR_QUIT(rwops);
   124         RWOP_ERR_QUIT(rwops);
   124     rwops->close(rwops);
   125     rwops->close(rwops);
   125     unlink(FBASENAME2);
   126     unlink(FBASENAME2);
   126     printf("test2 OK\n");
   127     SDL_Log("test2 OK\n");
   127 
   128 
   128 /* test 3 : creation, writing , reading, seeking,
   129 /* test 3 : creation, writing , reading, seeking,
   129             test : w mode, r mode, w+ mode
   130             test : w mode, r mode, w+ mode
   130  */
   131  */
   131     rwops = SDL_RWFromFile(FBASENAME1, "wb");   /* write only */
   132     rwops = SDL_RWFromFile(FBASENAME1, "wb");   /* write only */
   199     if (2 != rwops->read(rwops, test_buf, 10, 3))
   200     if (2 != rwops->read(rwops, test_buf, 10, 3))
   200         RWOP_ERR_QUIT(rwops);
   201         RWOP_ERR_QUIT(rwops);
   201     if (SDL_memcmp(test_buf, "12345678901234567890", 20))
   202     if (SDL_memcmp(test_buf, "12345678901234567890", 20))
   202         RWOP_ERR_QUIT(rwops);
   203         RWOP_ERR_QUIT(rwops);
   203     rwops->close(rwops);
   204     rwops->close(rwops);
   204     printf("test3 OK\n");
   205     SDL_Log("test3 OK\n");
   205 
   206 
   206 /* test 4: same in r+ mode */
   207 /* test 4: same in r+ mode */
   207     rwops = SDL_RWFromFile(FBASENAME1, "rb+");  /* write + read + file must exists, no truncation */
   208     rwops = SDL_RWFromFile(FBASENAME1, "rb+");  /* write + read + file must exists, no truncation */
   208     if (!rwops)
   209     if (!rwops)
   209         RWOP_ERR_QUIT(rwops);
   210         RWOP_ERR_QUIT(rwops);
   234     if (2 != rwops->read(rwops, test_buf, 10, 3))
   235     if (2 != rwops->read(rwops, test_buf, 10, 3))
   235         RWOP_ERR_QUIT(rwops);
   236         RWOP_ERR_QUIT(rwops);
   236     if (SDL_memcmp(test_buf, "12345678901234567890", 20))
   237     if (SDL_memcmp(test_buf, "12345678901234567890", 20))
   237         RWOP_ERR_QUIT(rwops);
   238         RWOP_ERR_QUIT(rwops);
   238     rwops->close(rwops);
   239     rwops->close(rwops);
   239     printf("test4 OK\n");
   240     SDL_Log("test4 OK\n");
   240 
   241 
   241 /* test5 : append mode */
   242 /* test5 : append mode */
   242     rwops = SDL_RWFromFile(FBASENAME1, "ab+");  /* write + read + append */
   243     rwops = SDL_RWFromFile(FBASENAME1, "ab+");  /* write + read + append */
   243     if (!rwops)
   244     if (!rwops)
   244         RWOP_ERR_QUIT(rwops);
   245         RWOP_ERR_QUIT(rwops);
   275     if (3 != rwops->read(rwops, test_buf, 10, 3))
   276     if (3 != rwops->read(rwops, test_buf, 10, 3))
   276         RWOP_ERR_QUIT(rwops);
   277         RWOP_ERR_QUIT(rwops);
   277     if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
   278     if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
   278         RWOP_ERR_QUIT(rwops);
   279         RWOP_ERR_QUIT(rwops);
   279     rwops->close(rwops);
   280     rwops->close(rwops);
   280     printf("test5 OK\n");
   281     SDL_Log("test5 OK\n");
   281     cleanup();
   282     cleanup();
   282     return 0;                   /* all ok */
   283     return 0;                   /* all ok */
   283 }
   284 }