Actual source code: ex37.c
1: static char help[] = "Test PetscFormatConvertGetSize().\n";
3: #include <petscsys.h>
4: #include <petscviewer.h>
6: PetscErrorCode TestPetscVSNPrintf(char *, size_t, size_t *, const char *, ...);
8: int main(int argc, char **argv)
9: {
10: size_t sz, fullLength;
11: char *newformatstr, buffer[128], longstr[256], superlongstr[10000];
12: const char *formatstr = "Greetings %D %3.2f %g\n";
13: PetscInt i, twentytwo = 22;
15: PetscFunctionBeginUser;
16: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
18: /* test that PetscFormatConvertGetSize() correctly counts needed amount of space */
19: PetscCall(PetscFormatConvertGetSize(formatstr, &sz));
20: if (PetscDefined(USE_64BIT_INDICES)) {
21: PetscCheck(sz == 29, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Format size %zu should be 29", sz);
22: } else {
23: PetscCheck(sz == 27, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Format size %zu should be 27", sz);
24: }
25: PetscCall(PetscMalloc1(sz, &newformatstr));
26: PetscCall(PetscFormatConvert(formatstr, newformatstr));
27: PetscCall(PetscPrintf(PETSC_COMM_WORLD, newformatstr, twentytwo, 3.47, 3.0));
28: PetscCall(PetscFree(newformatstr));
30: /* Test correct count is returned with %g format */
31: PetscCall(PetscSNPrintfCount(buffer, sizeof(buffer), "Test %g %g\n", &sz, 3.33, 2.7));
32: PetscCall(PetscStrlen(buffer, &fullLength));
33: PetscCheck(sz == fullLength + 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscSNPrintfCount() count should be %d it is %d", (int)fullLength + 1, (int)sz);
35: /* test that TestPetscVSNPrintf() fullLength argument returns required space for the string when buffer is long enough */
36: PetscCall(TestPetscVSNPrintf(buffer, sizeof(buffer), &fullLength, "Greetings %s", "This is my string"));
37: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "buffer :%s: fullLength %d\n", buffer, (int)fullLength));
39: /* test that TestPetscVSNPrintf() fullLength argument returns required space for the string when buffer is not long enough */
40: for (i = 0; i < 255; i++) longstr[i] = 's';
41: longstr[255] = 0;
42: PetscCall(TestPetscVSNPrintf(buffer, sizeof(buffer), &fullLength, "Greetings %s", longstr));
43: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "longstr fullLength %d\n", (int)fullLength));
45: /* test that PetscPrintf() works for strings longer than the default buffer size */
46: for (i = 0; i < 9998; i++) superlongstr[i] = 's';
47: superlongstr[9998] = 't';
48: superlongstr[9999] = 0;
49: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Greetings %s", superlongstr));
51: /* test that PetscSynchronizedPrintf() works for strings longer than the default buffer size */
52: PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "Greetings %s", superlongstr));
53: PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, stdout));
55: /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */
56: PetscCall(PetscSynchronizedFPrintf(PETSC_COMM_WORLD, stdout, "Greetings %s", superlongstr));
57: PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, stdout));
59: /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */
60: PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
61: PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "Greetings %s", superlongstr));
62: PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
63: PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
65: /* add new line to end of file so that diff does not warn about it being missing */
66: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\n"));
67: PetscCall(PetscFinalize());
68: return 0;
69: }
71: PetscErrorCode TestPetscVSNPrintf(char *str, size_t l_str, size_t *fullLength, const char *format, ...)
72: {
73: va_list Argp;
75: PetscFunctionBegin;
76: va_start(Argp, format);
77: PetscCall(PetscVSNPrintf(str, l_str, format, fullLength, Argp));
78: PetscFunctionReturn(PETSC_SUCCESS);
79: }
80: /*TEST
82: test:
83: nsize: 2
84: requires: defined(PETSC_HAVE_VA_COPY)
86: TEST*/