Actual source code: ex37.c


  2: static char help[] = "Test PetscFormatConvertGetSize().\n";

  4: #include <petscsys.h>
  5: #include <petscviewer.h>

  7: PetscErrorCode TestPetscVSNPrintf(char *, size_t, size_t *, const char *, ...);

  9: int main(int argc, char **argv)
 10: {
 11:   size_t      sz, fullLength;
 12:   char       *newformatstr, buffer[128], longstr[256], superlongstr[10000];
 13:   const char *formatstr = "Greetings %D %3.2f %g\n";
 14:   PetscInt    i, twentytwo = 22;

 17:   PetscInitialize(&argc, &argv, (char *)0, help);

 19:   /* test that PetscFormatConvertGetSize() correctly counts needed amount of space */
 20:   PetscFormatConvertGetSize(formatstr, &sz);
 21:   if (PetscDefined(USE_64BIT_INDICES)) {
 23:   } else {
 25:   }
 26:   PetscMalloc1(sz, &newformatstr);
 27:   PetscFormatConvert(formatstr, newformatstr);
 28:   PetscPrintf(PETSC_COMM_WORLD, newformatstr, twentytwo, 3.47, 3.0);
 29:   PetscFree(newformatstr);

 31:   /* Test correct count is returned with %g format */
 32:   PetscSNPrintfCount(buffer, sizeof(buffer), "Test %g %g\n", &sz, 3.33, 2.7);
 33:   PetscStrlen(buffer, &fullLength);

 36:   /* test that TestPetscVSNPrintf() fullLength argument returns required space for the string when buffer is long enough */
 37:   TestPetscVSNPrintf(buffer, sizeof(buffer), &fullLength, "Greetings %s", "This is my string");
 38:   PetscPrintf(PETSC_COMM_WORLD, "buffer :%s: fullLength %d\n", buffer, (int)fullLength);

 40:   /* test that TestPetscVSNPrintf() fullLength argument returns required space for the string when buffer is not long enough */
 41:   for (i = 0; i < 255; i++) longstr[i] = 's';
 42:   longstr[255] = 0;
 43:   TestPetscVSNPrintf(buffer, sizeof(buffer), &fullLength, "Greetings %s", longstr);
 44:   PetscPrintf(PETSC_COMM_WORLD, "longstr fullLength %d\n", (int)fullLength);

 46:   /* test that PetscPrintf() works for strings longer than the default buffer size */
 47:   for (i = 0; i < 9998; i++) superlongstr[i] = 's';
 48:   superlongstr[9998] = 't';
 49:   superlongstr[9999] = 0;
 50:   PetscPrintf(PETSC_COMM_WORLD, "Greetings %s", superlongstr);

 52:   /* test that PetscSynchronizedPrintf() works for strings longer than the default buffer size */
 53:   PetscSynchronizedPrintf(PETSC_COMM_WORLD, "Greetings %s", superlongstr);
 54:   PetscSynchronizedFlush(PETSC_COMM_WORLD, stdout);

 56:   /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */
 57:   PetscSynchronizedFPrintf(PETSC_COMM_WORLD, stdout, "Greetings %s", superlongstr);
 58:   PetscSynchronizedFlush(PETSC_COMM_WORLD, stdout);

 60:   /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */
 61:   PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);
 62:   PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "Greetings %s", superlongstr);
 63:   PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
 64:   PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);

 66:   /* add new line to end of file so that diff does not warn about it being missing */
 67:   PetscPrintf(PETSC_COMM_WORLD, "\n");
 68:   PetscFinalize();
 69:   return 0;
 70: }

 72: PetscErrorCode TestPetscVSNPrintf(char *str, size_t l_str, size_t *fullLength, const char *format, ...)
 73: {
 74:   va_list Argp;

 76:   va_start(Argp, format);
 77:   PetscVSNPrintf(str, l_str, format, fullLength, Argp);
 78:   return 0;
 79: }
 80: /*TEST

 82:    test:
 83:      nsize: 2
 84:      requires: defined(PETSC_HAVE_VA_COPY)

 86: TEST*/