test/test_physfs.c
changeset 508 0e75524a96af
parent 500 6de777c231e3
child 528 4d84e1994edb
equal deleted inserted replaced
507:e13baf8efaaa 508:0e75524a96af
    32 #define TEST_VERSION_MAJOR  0
    32 #define TEST_VERSION_MAJOR  0
    33 #define TEST_VERSION_MINOR  1
    33 #define TEST_VERSION_MINOR  1
    34 #define TEST_VERSION_PATCH  7
    34 #define TEST_VERSION_PATCH  7
    35 
    35 
    36 static FILE *history_file = NULL;
    36 static FILE *history_file = NULL;
       
    37 static PHYSFS_uint32 do_buffer_size = 0;
    37 
    38 
    38 static void output_versions(void)
    39 static void output_versions(void)
    39 {
    40 {
    40     PHYSFS_Version compiled;
    41     PHYSFS_Version compiled;
    41     PHYSFS_Version linked;
    42     PHYSFS_Version linked;
   286     printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
   287     printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
   287     return(1);
   288     return(1);
   288 } /* cmd_permitsyms */
   289 } /* cmd_permitsyms */
   289 
   290 
   290 
   291 
       
   292 static int cmd_setbuffer(char *args)
       
   293 {
       
   294     if (*args == '\"')
       
   295     {
       
   296         args++;
       
   297         args[strlen(args) - 1] = '\0';
       
   298     } /* if */
       
   299 
       
   300     do_buffer_size = atoi(args);
       
   301     if (do_buffer_size)
       
   302     {
       
   303         printf("Further tests will set a (%lu) size buffer.\n",
       
   304                 (unsigned long) do_buffer_size);
       
   305     } /* if */
       
   306 
       
   307     else
       
   308     {
       
   309         printf("Further tests will NOT use a buffer.\n");
       
   310     } /* else */
       
   311 
       
   312     return(1);
       
   313 } /* cmd_setbuffer */
       
   314 
       
   315 
       
   316 static int cmd_stressbuffer(char *args)
       
   317 {
       
   318     int num;
       
   319 
       
   320     if (*args == '\"')
       
   321     {
       
   322         args++;
       
   323         args[strlen(args) - 1] = '\0';
       
   324     } /* if */
       
   325 
       
   326     num = atoi(args);
       
   327     if (num < 0)
       
   328         printf("buffer must be greater than or equal to zero.\n");
       
   329     else
       
   330     {
       
   331         PHYSFS_file *f;
       
   332         int rndnum;
       
   333 
       
   334         printf("Stress testing with (%d) byte buffer...\n", num);
       
   335         f = PHYSFS_openWrite("test.txt");
       
   336         if (f == NULL)
       
   337             printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
       
   338         else
       
   339         {
       
   340             int i, j;
       
   341             char buf[37];
       
   342             char buf2[37];
       
   343 
       
   344             if (!PHYSFS_setBuffer(f, num))
       
   345             {
       
   346                 printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
       
   347                 PHYSFS_close(f);
       
   348                 PHYSFS_delete("test.txt");
       
   349                 return(1);
       
   350             } /* if */
       
   351 
       
   352             strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
       
   353             srand(time(NULL));
       
   354 
       
   355             for (i = 0; i < 10; i++)
       
   356             {
       
   357                 for (j = 0; j < 10000; j++)
       
   358                 {
       
   359                     int right = 1 + (int) (35.0 * rand() / (RAND_MAX + 1.0));
       
   360                     int left = 36 - right;
       
   361                     if (PHYSFS_write(f, buf, left, 1) != 1)
       
   362                     {
       
   363                         printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
       
   364                         PHYSFS_close(f);
       
   365                         return(1);
       
   366                     } /* if */
       
   367 
       
   368                     rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
       
   369                     if (rndnum == 42)
       
   370                     {
       
   371                         if (!PHYSFS_flush(f))
       
   372                         {
       
   373                             printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
       
   374                             PHYSFS_close(f);
       
   375                             return(1);
       
   376                         } /* if */
       
   377                     } /* if */
       
   378 
       
   379                     if (PHYSFS_write(f, buf + left, 1, right) != right)
       
   380                     {
       
   381                         printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
       
   382                         PHYSFS_close(f);
       
   383                         return(1);
       
   384                     } /* if */
       
   385 
       
   386                     rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
       
   387                     if (rndnum == 42)
       
   388                     {
       
   389                         if (!PHYSFS_flush(f))
       
   390                         {
       
   391                             printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
       
   392                             PHYSFS_close(f);
       
   393                             return(1);
       
   394                         } /* if */
       
   395                     } /* if */
       
   396                 } /* for */
       
   397 
       
   398                 if (!PHYSFS_flush(f))
       
   399                 {
       
   400                     printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
       
   401                     PHYSFS_close(f);
       
   402                     return(1);
       
   403                 } /* if */
       
   404 
       
   405             } /* for */
       
   406 
       
   407             if (!PHYSFS_close(f))
       
   408             {
       
   409                 printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
       
   410                 return(1);  /* oh well. */
       
   411             } /* if */
       
   412 
       
   413             printf(" ... test file written ...\n");
       
   414             f = PHYSFS_openRead("test.txt");
       
   415             if (f == NULL)
       
   416             {
       
   417                 printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
       
   418                 return(1);
       
   419             } /* if */
       
   420 
       
   421             if (!PHYSFS_setBuffer(f, num))
       
   422             {
       
   423                 printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
       
   424                 PHYSFS_close(f);
       
   425                 return(1);
       
   426             } /* if */
       
   427 
       
   428             for (i = 0; i < 10; i++)
       
   429             {
       
   430                 for (j = 0; j < 10000; j++)
       
   431                 {
       
   432                     int right = 1 + (int) (35.0 * rand() / (RAND_MAX + 1.0));
       
   433                     int left = 36 - right;
       
   434                     if (PHYSFS_read(f, buf2, left, 1) != 1)
       
   435                     {
       
   436                         printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
       
   437                         PHYSFS_close(f);
       
   438                         return(1);
       
   439                     } /* if */
       
   440 
       
   441                     rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
       
   442                     if (rndnum == 42)
       
   443                     {
       
   444                         if (!PHYSFS_flush(f))
       
   445                         {
       
   446                             printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
       
   447                             PHYSFS_close(f);
       
   448                             return(1);
       
   449                         } /* if */
       
   450                     } /* if */
       
   451 
       
   452                     if (PHYSFS_read(f, buf2 + left, 1, right) != right)
       
   453                     {
       
   454                         printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
       
   455                         PHYSFS_close(f);
       
   456                         return(1);
       
   457                     } /* if */
       
   458 
       
   459                     rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
       
   460                     if (rndnum == 42)
       
   461                     {
       
   462                         if (!PHYSFS_flush(f))
       
   463                         {
       
   464                             printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
       
   465                             PHYSFS_close(f);
       
   466                             return(1);
       
   467                         } /* if */
       
   468                     } /* if */
       
   469 
       
   470                     if (memcmp(buf, buf2, 36) != 0)
       
   471                     {
       
   472                         printf("readback is mismatched on iterations (%d, %d).\n", i, j);
       
   473                         printf("wanted: [");
       
   474                         for (i = 0; i < 36; i++)
       
   475                             printf("%c", buf[i]);
       
   476                         printf("]\n");
       
   477 
       
   478                         printf("   got: [");
       
   479                         for (i = 0; i < 36; i++)
       
   480                             printf("%c", buf2[i]);
       
   481                         printf("]\n");
       
   482                         PHYSFS_close(f);
       
   483                         return(1);
       
   484                     } /* if */
       
   485                 } /* for */
       
   486 
       
   487                 if (!PHYSFS_flush(f))
       
   488                 {
       
   489                     printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
       
   490                     PHYSFS_close(f);
       
   491                     return(1);
       
   492                 } /* if */
       
   493 
       
   494             } /* for */
       
   495 
       
   496             printf(" ... test file read ...\n");
       
   497 
       
   498             if (!PHYSFS_eof(f))
       
   499                 printf("PHYSFS_eof() returned true! That's wrong.\n");
       
   500 
       
   501             if (!PHYSFS_close(f))
       
   502             {
       
   503                 printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
       
   504                 return(1);  /* oh well. */
       
   505             } /* if */
       
   506 
       
   507             PHYSFS_delete("test.txt");
       
   508             printf("stress test completed successfully.\n");
       
   509         } /* else */
       
   510     } /* else */
       
   511 
       
   512     return(1);
       
   513 } /* cmd_stressbuffer */
       
   514 
       
   515 
   291 static int cmd_setsaneconfig(char *args)
   516 static int cmd_setsaneconfig(char *args)
   292 {
   517 {
   293     char *org;
   518     char *org;
   294     char *appName;
   519     char *appName;
   295     char *arcExt;
   520     char *arcExt;
   431     f = PHYSFS_openRead(args);
   656     f = PHYSFS_openRead(args);
   432     if (f == NULL)
   657     if (f == NULL)
   433         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   658         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   434     else
   659     else
   435     {
   660     {
       
   661         if (do_buffer_size)
       
   662         {
       
   663             if (!PHYSFS_setBuffer(f, do_buffer_size))
       
   664             {
       
   665                 printf("failed to set file buffer. Reason: [%s].\n",
       
   666                         PHYSFS_getLastError());
       
   667                 PHYSFS_close(f);
       
   668                 return(1);
       
   669             } /* if */
       
   670         } /* if */
       
   671 
   436         while (1)
   672         while (1)
   437         {
   673         {
   438             char buffer[128];
   674             char buffer[128];
   439             PHYSFS_sint64 rc;
   675             PHYSFS_sint64 rc;
   440             PHYSFS_sint64 i;
   676             PHYSFS_sint64 i;
   504     f = PHYSFS_openAppend(args);
   740     f = PHYSFS_openAppend(args);
   505     if (f == NULL)
   741     if (f == NULL)
   506         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   742         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   507     else
   743     else
   508     {
   744     {
   509         size_t bw = strlen(WRITESTR);
   745         size_t bw;
   510         PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
   746         PHYSFS_sint64 rc;
       
   747 
       
   748         if (do_buffer_size)
       
   749         {
       
   750             if (!PHYSFS_setBuffer(f, do_buffer_size))
       
   751             {
       
   752                 printf("failed to set file buffer. Reason: [%s].\n",
       
   753                         PHYSFS_getLastError());
       
   754                 PHYSFS_close(f);
       
   755                 return(1);
       
   756             } /* if */
       
   757         } /* if */
       
   758 
       
   759         bw = strlen(WRITESTR);
       
   760         rc = PHYSFS_write(f, WRITESTR, 1, bw);
   511         if (rc != bw)
   761         if (rc != bw)
   512         {
   762         {
   513             printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
   763             printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
   514                    (int) rc, (int) bw, PHYSFS_getLastError());
   764                    (int) rc, (int) bw, PHYSFS_getLastError());
   515         } /* if */
   765         } /* if */
   538     f = PHYSFS_openWrite(args);
   788     f = PHYSFS_openWrite(args);
   539     if (f == NULL)
   789     if (f == NULL)
   540         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   790         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   541     else
   791     else
   542     {
   792     {
   543         size_t bw = strlen(WRITESTR);
   793         size_t bw;
   544         PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
   794         PHYSFS_sint64 rc;
       
   795 
       
   796         if (do_buffer_size)
       
   797         {
       
   798             if (!PHYSFS_setBuffer(f, do_buffer_size))
       
   799             {
       
   800                 printf("failed to set file buffer. Reason: [%s].\n",
       
   801                         PHYSFS_getLastError());
       
   802                 PHYSFS_close(f);
       
   803                 return(1);
       
   804             } /* if */
       
   805         } /* if */
       
   806 
       
   807         bw = strlen(WRITESTR);
       
   808         rc = PHYSFS_write(f, WRITESTR, 1, bw);
   545         if (rc != bw)
   809         if (rc != bw)
   546         {
   810         {
   547             printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
   811             printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
   548                    (int) rc, (int) bw, PHYSFS_getLastError());
   812                    (int) rc, (int) bw, PHYSFS_getLastError());
   549         } /* if */
   813         } /* if */
   646     { "cat",            cmd_cat,            1, "<fileToCat>"                },
   910     { "cat",            cmd_cat,            1, "<fileToCat>"                },
   647     { "filelength",     cmd_filelength,     1, "<fileToCheck>"              },
   911     { "filelength",     cmd_filelength,     1, "<fileToCheck>"              },
   648     { "append",         cmd_append,         1, "<fileToAppend>"             },
   912     { "append",         cmd_append,         1, "<fileToAppend>"             },
   649     { "write",          cmd_write,          1, "<fileToCreateOrTrash>"      },
   913     { "write",          cmd_write,          1, "<fileToCreateOrTrash>"      },
   650     { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>"            },
   914     { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>"            },
       
   915     { "setbuffer",      cmd_setbuffer,      1, "<bufferSize>"               },
       
   916     { "stressbuffer",   cmd_stressbuffer,   1, "<bufferSize>"               },
   651     { NULL,             NULL,              -1, NULL                         }
   917     { NULL,             NULL,              -1, NULL                         }
   652 };
   918 };
   653 
   919 
   654 
   920 
   655 static void output_usage(const char *intro, const command_info *cmdinfo)
   921 static void output_usage(const char *intro, const command_info *cmdinfo)