Actual source code: ex37.c

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

  3: #include <petscsys.h>

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

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

 14:   PetscFunctionBeginUser;
 15:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

 17:   /* test that PetscFormatConvertGetSize() correctly counts needed amount of space */
 18:   PetscCall(PetscFormatConvertGetSize(formatstr, &sz));
 19:   if (PetscDefined(USE_64BIT_INDICES)) {
 20:     PetscCheck(sz == 29, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Format size %zu should be 29", sz);
 21:   } else {
 22:     PetscCheck(sz == 27, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Format size %zu should be 27", sz);
 23:   }
 24:   PetscCall(PetscMalloc1(sz, &newformatstr));
 25:   PetscCall(PetscFormatConvert(formatstr, newformatstr));
 26:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, newformatstr, twentytwo, 3.47, 3.0));
 27:   PetscCall(PetscFree(newformatstr));

 29:   /* Test correct count is returned with %g format */
 30:   PetscCall(PetscSNPrintfCount(buffer, sizeof(buffer), "Test %g %g\n", &sz, 3.33, 2.7));
 31:   PetscCall(PetscStrlen(buffer, &fullLength));
 32:   PetscCheck(sz == fullLength + 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscSNPrintfCount() count should be %d it is %d", (int)fullLength + 1, (int)sz);

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

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

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

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

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

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

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

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

 74:   PetscFunctionBegin;
 75:   va_start(Argp, format);
 76:   PetscCall(PetscVSNPrintf(str, l_str, format, fullLength, Argp));
 77:   PetscFunctionReturn(PETSC_SUCCESS);
 78: }
 79: /*TEST

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

 85: TEST*/