Debian Codegraph @

nsprpub/pr/tests/prfz.c

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /*
6 * This is a simple test of the PR_fprintf() function for size_t formats.
7 */
8
9 #include "prprf.h"
10 #include <sys/types.h>
11 #include <limits.h>
12 #include <string.h>
13 #include <stdint.h>
14
15 int main(int argc, char** argv) {
16 char buffer[128];
17
18 size_t unsigned_small = 266;
19 #ifdef XP_UNIX
20 ssize_t signed_small_p = 943;
21 ssize_t signed_small_n = -1;
22 #endif
23 size_t unsigned_max = SIZE_MAX;
24 size_t unsigned_min = 0;
25 #ifdef XP_UNIX
26 ssize_t signed_max = SSIZE_MAX;
27 #endif
28
29 printf("Test: unsigned small '%%zu' : ");
30 PR_snprintf(buffer, sizeof(buffer), "%zu", unsigned_small);
31 if (strncmp(buffer, "266", sizeof(buffer)) != 0) {
32 printf("Failed, got '%s'\n", buffer);
33 return -1;
34 }
35 printf("OK\n");
36
37 #ifdef XP_UNIX
38 printf("Test: signed small positive '%%zd' : ");
39 PR_snprintf(buffer, sizeof(buffer), "%zd", signed_small_p);
40 if (strncmp(buffer, "943", sizeof(buffer)) != 0) {
41 printf("Failed, got '%s'\n", buffer);
42 return -1;
43 }
44 printf("OK\n");
45
46 printf("Test: signed small negative '%%zd' : ");
47 PR_snprintf(buffer, sizeof(buffer), "%zd", signed_small_n);
48 if (strncmp(buffer, "-1", sizeof(buffer)) != 0) {
49 printf("Failed, got '%s'\n", buffer);
50 return -1;
51 }
52 printf("OK\n");
53 #endif
54
55 printf("Test: 0 '%%zu' : ");
56 PR_snprintf(buffer, sizeof(buffer), "%zu", unsigned_min);
57 if (strncmp(buffer, "0", sizeof(buffer)) != 0) {
58 printf("Failed, got '%s'\n", buffer);
59 return -1;
60 }
61 printf("OK\n");
62
63 printf("Test: SIZE_MAX '%%zx' : ");
64 PR_snprintf(buffer, sizeof(buffer), "%zx", unsigned_max);
65 if (strspn(buffer, "f") != sizeof(size_t) * 2) {
66 printf("Failed, got '%s'\n", buffer);
67 return -1;
68 }
69 printf("OK\n");
70
71 #ifdef XP_UNIX
72 printf("Test: SSIZE_MAX '%%zx' : ");
73 PR_snprintf(buffer, sizeof(buffer), "%zx", signed_max);
74 if (*buffer != '7' || strspn(buffer + 1, "f") != sizeof(ssize_t) * 2 - 1) {
75 printf("Failed, got '%s'\n", buffer);
76 return -1;
77 }
78 printf("OK\n");
79 #endif
80
81 return 0;
82 }