1 2 static char help[] = "Test PetscFormatConvertGetSize().\n"; 3 4 #include <petscsys.h> 5 #include <petscviewer.h> 6 7 PetscErrorCode TestPetscVSNPrintf(char*,size_t,size_t*,const char*,...); 8 9 int main(int argc,char **argv) 10 { 11 PetscErrorCode ierr; 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; 16 17 ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; 18 19 /* test that PetscFormatConvertGetSize() correctly counts needed amount of space */ 20 ierr = PetscFormatConvertGetSize(formatstr,&sz);CHKERRQ(ierr); 21 if (PetscDefined(USE_64BIT_INDICES)) { 22 PetscCheckFalse(sz != 29,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Format size %zu should be 29",sz); 23 } else { 24 PetscCheckFalse(sz != 27,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Format size %zu should be 27",sz); 25 } 26 ierr = PetscMalloc1(sz,&newformatstr);CHKERRQ(ierr); 27 ierr = PetscFormatConvert(formatstr,newformatstr);CHKERRQ(ierr); 28 ierr = PetscPrintf(PETSC_COMM_WORLD,newformatstr,twentytwo,3.47,3.0);CHKERRQ(ierr); 29 ierr = PetscFree(newformatstr);CHKERRQ(ierr); 30 31 /* Test correct count is returned with %g format */ 32 ierr = PetscSNPrintfCount(buffer,sizeof(buffer),"Test %g %g\n",&sz,3.33,2.7);CHKERRQ(ierr); 33 ierr = PetscStrlen(buffer,&fullLength);CHKERRQ(ierr); 34 PetscCheckFalse(sz != fullLength+1,PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscSNPrintfCount() count should be %d it is %d",(int)fullLength+1,(int)sz); 35 36 /* test that TestPetscVSNPrintf() fullLength argument returns required space for the string when buffer is long enough */ 37 ierr = TestPetscVSNPrintf(buffer,sizeof(buffer),&fullLength,"Greetings %s","This is my string");CHKERRQ(ierr); 38 ierr = PetscPrintf(PETSC_COMM_WORLD,"buffer :%s: fullLength %d\n",buffer,(int)fullLength);CHKERRQ(ierr); 39 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 ierr = TestPetscVSNPrintf(buffer,sizeof(buffer),&fullLength,"Greetings %s",longstr);CHKERRQ(ierr); 43 ierr = PetscPrintf(PETSC_COMM_WORLD,"longstr fullLength %d\n",(int)fullLength);CHKERRQ(ierr); 44 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 ierr = PetscPrintf(PETSC_COMM_WORLD,"Greetings %s",superlongstr);CHKERRQ(ierr); 48 49 /* test that PetscSynchronizedPrintf() works for strings longer than the default buffer size */ 50 ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD,"Greetings %s",superlongstr);CHKERRQ(ierr); 51 ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD,stdout);CHKERRQ(ierr); 52 53 /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */ 54 ierr = PetscSynchronizedFPrintf(PETSC_COMM_WORLD,stdout,"Greetings %s",superlongstr);CHKERRQ(ierr); 55 ierr = PetscSynchronizedFlush(PETSC_COMM_WORLD,stdout);CHKERRQ(ierr); 56 57 /* test that PetscSynchronizedFPrintf() works for strings longer than the default buffer size */ 58 ierr = PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 59 ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"Greetings %s",superlongstr);CHKERRQ(ierr); 60 ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 61 ierr = PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 62 63 /* add new line to end of file so that diff does not warn about it being missing */ 64 ierr = PetscPrintf(PETSC_COMM_WORLD,"\n");CHKERRQ(ierr); 65 ierr = PetscFinalize(); 66 return ierr; 67 } 68 69 PetscErrorCode TestPetscVSNPrintf(char *str,size_t l_str,size_t *fullLength,const char* format,...) 70 { 71 va_list Argp; 72 PetscErrorCode ierr; 73 74 PetscFunctionBegin; 75 va_start(Argp,format); 76 ierr = PetscVSNPrintf(str,l_str,format,fullLength,Argp);CHKERRQ(ierr); 77 PetscFunctionReturn(0); 78 } 79 /*TEST 80 81 test: 82 nsize: 2 83 requires: defined(PETSC_HAVE_VA_COPY) 84 85 TEST*/ 86