| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================================ |
| 6 | ** |
| 7 | ** Source: test19.c (fprintf) |
| 8 | ** |
| 9 | ** Purpose: Tests the variable length precision argument. |
| 10 | ** This test is modeled after the fprintf series. |
| 11 | ** |
| 12 | ** |
| 13 | **==========================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | #include "../fprintf.h" |
| 17 | |
| 18 | /* |
| 19 | * Depends on memcmp, strlen, fopen, fseek and fgets. |
| 20 | */ |
| 21 | |
| 22 | #define DOTEST(a,b,c,d,e,f) DoTest(a,b,(void*)c,d,e,f) |
| 23 | |
| 24 | void DoTest(char *formatstr, int precision, void *param, |
| 25 | char *paramstr, char *checkstr1, char *checkstr2) |
| 26 | { |
| 27 | FILE *fp; |
| 28 | char buf[256]; |
| 29 | |
| 30 | if ((fp = fopen("testfile.txt" , "w+" )) == NULL ) |
| 31 | { |
| 32 | Fail("ERROR: fopen failed to create testfile\n" ); |
| 33 | } |
| 34 | |
| 35 | if ((fprintf(fp, formatstr, precision, param)) < 0) |
| 36 | { |
| 37 | Fail("ERROR: fprintf failed\n" ); |
| 38 | } |
| 39 | |
| 40 | if ((fseek(fp, 0, SEEK_SET)) != 0) |
| 41 | { |
| 42 | Fail("ERROR: fseek failed\n" ); |
| 43 | } |
| 44 | |
| 45 | if ((fgets(buf, 100, fp)) == NULL) |
| 46 | { |
| 47 | Fail("ERROR: fseek failed\n" ); |
| 48 | } |
| 49 | |
| 50 | if (memcmp(buf, checkstr1, strlen(buf) + 1) != 0 && |
| 51 | memcmp(buf, checkstr2, strlen(buf) + 1) != 0) |
| 52 | { |
| 53 | Fail("ERROR: failed to insert %s into \"%s\" with precision %d\n" |
| 54 | "Expected \"%s\" or \"%s\", got \"%s\".\n" , paramstr, formatstr, |
| 55 | precision, |
| 56 | checkstr1, checkstr2, buf); |
| 57 | } |
| 58 | |
| 59 | if ((fclose( fp )) != 0) |
| 60 | |
| 61 | { |
| 62 | Fail("ERROR: fclose failed to close \"testfile.txt\"\n" ); |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | void DoublePrecTest(char *formatstr, int precision, |
| 68 | double param, char *checkstr1, char *checkstr2) |
| 69 | { |
| 70 | FILE *fp; |
| 71 | char buf[256]; |
| 72 | |
| 73 | if ((fp = fopen("testfile.txt" , "w+" )) == NULL ) |
| 74 | { |
| 75 | Fail("ERROR: fopen failed to create testfile\n" ); |
| 76 | } |
| 77 | |
| 78 | if ((fprintf(fp, formatstr, precision, param)) < 0) |
| 79 | { |
| 80 | Fail("ERROR: fprintf failed\n" ); |
| 81 | } |
| 82 | |
| 83 | if ((fseek(fp, 0, SEEK_SET)) != 0) |
| 84 | { |
| 85 | Fail("ERROR: fseek failed\n" ); |
| 86 | } |
| 87 | |
| 88 | if ((fgets(buf, 100, fp)) == NULL) |
| 89 | { |
| 90 | Fail("ERROR: fseek failed\n" ); |
| 91 | } |
| 92 | |
| 93 | if (memcmp(buf, checkstr1, strlen(buf) + 1) != 0 && |
| 94 | memcmp(buf, checkstr2, strlen(buf) + 1) != 0) |
| 95 | { |
| 96 | Fail("ERROR: failed to insert %f into \"%s\" with precision %d\n" |
| 97 | "Expected \"%s\" or \"%s\", got \"%s\".\n" , |
| 98 | param, formatstr, precision, checkstr1, checkstr2, buf); |
| 99 | } |
| 100 | |
| 101 | if ((fclose( fp )) != 0) |
| 102 | { |
| 103 | Fail("ERROR: fclose failed to close \"testfile.txt\"\n" ); |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | int __cdecl main(int argc, char *argv[]) |
| 109 | { |
| 110 | |
| 111 | if (PAL_Initialize(argc, argv) != 0) |
| 112 | return(FAIL); |
| 113 | |
| 114 | DOTEST("%.*s" , 2, "bar" , "bar" , "ba" , "ba" ); |
| 115 | DOTEST("%.*S" , 2, convert("bar" ), "bar" , "ba" , "ba" ); |
| 116 | |
| 117 | //DOTEST("%.*n", 4, 2, "2", "0002"); |
| 118 | DOTEST("%.*c" , 0, 'a', "a" , "a" , "a" ); |
| 119 | DOTEST("%.*c" , 4, 'a', "a" , "a" , "a" ); |
| 120 | DOTEST("%.*C" , 0, (WCHAR)'a', "a" , "a" , "a" ); |
| 121 | DOTEST("%.*C" , 4, (WCHAR)'a', "a" , "a" , "a" ); |
| 122 | DOTEST("%.*d" , 1, 42, "42" , "42" , "42" ); |
| 123 | DOTEST("%.*d" , 3, 42, "42" , "042" , "042" ); |
| 124 | DOTEST("%.*i" , 1, 42, "42" , "42" , "42" ); |
| 125 | DOTEST("%.*i" , 3, 42, "42" , "042" , "042" ); |
| 126 | DOTEST("%.*o" , 1, 42, "42" , "52" , "52" ); |
| 127 | DOTEST("%.*o" , 3, 42, "42" , "052" , "052" ); |
| 128 | DOTEST("%.*u" , 1, 42, "42" , "42" , "42" ); |
| 129 | DOTEST("%.*u" , 3, 42, "42" , "042" , "042" ); |
| 130 | DOTEST("%.*x" , 1, 0x42, "0x42" , "42" , "42" ); |
| 131 | DOTEST("%.*x" , 3, 0x42, "0x42" , "042" , "042" ); |
| 132 | DOTEST("%.*X" , 1, 0x42, "0x42" , "42" , "42" ); |
| 133 | DOTEST("%.*X" , 3, 0x42, "0x42" , "042" , "042" ); |
| 134 | |
| 135 | |
| 136 | DoublePrecTest("%.*e" , 1, 2.01, "2.0e+000" , "2.0e+00" ); |
| 137 | DoublePrecTest("%.*e" , 3, 2.01, "2.010e+000" , "2.010e+00" ); |
| 138 | DoublePrecTest("%.*E" , 1, 2.01, "2.0E+000" , "2.0E+00" ); |
| 139 | DoublePrecTest("%.*E" , 3, 2.01, "2.010E+000" , "2.010E+00" ); |
| 140 | DoublePrecTest("%.*f" , 1, 2.01, "2.0" , "2.0" ); |
| 141 | DoublePrecTest("%.*f" , 3, 2.01, "2.010" , "2.010" ); |
| 142 | DoublePrecTest("%.*g" , 1, 256.01, "3e+002" , "3e+02" ); |
| 143 | DoublePrecTest("%.*g" , 3, 256.01, "256" , "256" ); |
| 144 | DoublePrecTest("%.*g" , 4, 256.01, "256" , "256" ); |
| 145 | DoublePrecTest("%.*g" , 6, 256.01, "256.01" , "256.01" ); |
| 146 | DoublePrecTest("%.*G" , 1, 256.01, "3E+002" , "3E+02" ); |
| 147 | DoublePrecTest("%.*G" , 3, 256.01, "256" , "256" ); |
| 148 | DoublePrecTest("%.*G" , 4, 256.01, "256" , "256" ); |
| 149 | DoublePrecTest("%.*G" , 6, 256.01, "256.01" , "256.01" ); |
| 150 | |
| 151 | PAL_Terminate(); |
| 152 | return PASS; |
| 153 | } |
| 154 | |