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;

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

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

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

 35:   /* test that TestPetscVSNPrintf() fullLength argument returns required space for the string when buffer is long enough */
 36:   TestPetscVSNPrintf(buffer,sizeof(buffer),&fullLength,"Greetings %s","This is my string");
 37:   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';} longstr[255] = 0;
 41:   TestPetscVSNPrintf(buffer,sizeof(buffer),&fullLength,"Greetings %s",longstr);
 42:   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';} superlongstr[9998] = 't'; superlongstr[9999] = 0;
 46:   PetscPrintf(PETSC_COMM_WORLD,"Greetings %s",superlongstr);

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

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

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

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

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

 72:   va_start(Argp,format);
 73:   PetscVSNPrintf(str,l_str,format,fullLength,Argp);
 74:   return 0;
 75: }
 76: /*TEST

 78:    test:
 79:      nsize: 2
 80:      requires: defined(PETSC_HAVE_VA_COPY)

 82: TEST*/