Actual source code: ex37.c
petsc-3.13.6 2020-09-29
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: {
12: size_t sz,fullLength;
13: char *newformatstr,buffer[128],longstr[256],superlongstr[10000];
14: const char *formatstr = "Greetings %D %3.2f %g\n";
15: PetscInt i,twentytwo = 22;
17: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
19: /* test that PetscFormatConvertGetSize() correctly counts needed amount of space */
20: PetscFormatConvertGetSize(formatstr,&sz);
21: #if !defined(PETSC_USE_64BIT_INDICES)
22: if (sz != 27) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Format size %d should be 27\n",(int)sz);
23: #else
24: if (sz != 29) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Format size %d should be 29\n",(int)sz);
25: #endif
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);
34: if (sz != fullLength+1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscSNPrintfCount() count should be %d it is %d\n",(int)fullLength+1,(int)sz);
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';} longstr[255] = 0;
42: TestPetscVSNPrintf(buffer,sizeof(buffer),&fullLength,"Greetings %s",longstr);
43: 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';} superlongstr[9998] = 't'; superlongstr[9999] = 0;
47: PetscPrintf(PETSC_COMM_WORLD,"Greetings %s",superlongstr);
49: /* test that PetscSynchronizedPrintf() works for strings longer than the default buffer size */
50: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"Greetings %s",superlongstr);
51: PetscSynchronizedFlush(PETSC_COMM_WORLD,stdout);
53: /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */
54: PetscSynchronizedFPrintf(PETSC_COMM_WORLD,stdout,"Greetings %s",superlongstr);
55: PetscSynchronizedFlush(PETSC_COMM_WORLD,stdout);
57: /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */
58: PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);
59: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"Greetings %s",superlongstr);
60: PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
61: PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);
63: /* add new line to end of file so that diff does not warn about it being missing */
64: PetscPrintf(PETSC_COMM_WORLD,"\n");
65: PetscFinalize();
66: return ierr;
67: }
69: PetscErrorCode TestPetscVSNPrintf(char *str,size_t l_str,size_t *fullLength,const char* format,...)
70: {
71: va_list Argp;
75: va_start(Argp,format);
76: PetscVSNPrintf(str,l_str,format,fullLength,Argp);
77: return(0);
78: }
79: /*TEST
81: test:
82: nsize: 2
83: requires: define(PETSC_HAVE_VA_COPY)
85: TEST*/