1 /* 2 * Copyright (c) 2020 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 #include <darwintest.h> 30 #include <darwintest_utils.h> 31 32 // This can either test libkern's sscanf, or stdio.h's. 33 #define TEST_LIBKERN 34 35 #if defined(TEST_LIBKERN) 36 static int libkern_isspace(char c); 37 int libkern_sscanf(const char *ibuf, const char *fmt, ...); 38 int libkern_vsscanf(const char *inp, char const *fmt0, va_list ap); 39 # define isspace(C) libkern_isspace(C) 40 # define sscanf(...) libkern_sscanf(__VA_ARGS__) 41 # define vsscanf(...) libkern_vsscanf(__VA_ARGS__) 42 # include "../libkern/stdio/scanf.c" 43 #else 44 # include <stdio.h> 45 #endif 46 47 T_DECL(scanf_empty, "empty") 48 { 49 T_ASSERT_EQ_INT(sscanf("", ""), 0, "empty input and format"); 50 T_ASSERT_EQ_INT(sscanf("", "match me"), EOF, "empty input"); 51 T_ASSERT_EQ_INT(sscanf("lonely", ""), 0, "empty format"); 52 } 53 54 T_DECL(scanf_percent, "percent") 55 { 56 T_ASSERT_EQ_INT(sscanf("%", "%%"), 0, "two percent"); 57 } 58 59 T_DECL(scanf_character, "character") 60 { 61 char c; 62 for (char i = ' '; i <= '~'; ++i) { 63 char buf[] = { i, '\0' }; 64 T_ASSERT_EQ_INT(sscanf(buf, "%c", &c), 1, "character matched"); 65 T_ASSERT_EQ_INT(c, i, "character value"); 66 } 67 } 68 69 T_DECL(scanf_characters, "characters") 70 { 71 char c[] = { 'a', 'b', 'c', 'd', 'e' }; 72 T_ASSERT_EQ_INT(sscanf("01234", "%4c", c), 1, "characters matched"); 73 T_ASSERT_EQ_INT(c[0], '0', "characters value"); 74 T_ASSERT_EQ_INT(c[1], '1', "characters value"); 75 T_ASSERT_EQ_INT(c[2], '2', "characters value"); 76 T_ASSERT_EQ_INT(c[3], '3', "characters value"); 77 T_ASSERT_EQ_INT(c[4], 'e', "characters value wasn't overwritten"); 78 } 79 80 T_DECL(scanf_string, "string") 81 { 82 char s[] = { 'a', 'b', 'c', 'd', 'e' }; 83 T_ASSERT_EQ_INT(sscanf("012", "%s", s), 1, "string matched"); 84 T_ASSERT_EQ_STR(s, "012", "string value"); 85 T_ASSERT_EQ_INT(s[4], 'e', "string value wasn't overwritten"); 86 T_ASSERT_EQ_INT(sscanf("ABCDE", "%3s", s), 1, "string matched"); 87 T_ASSERT_EQ_STR(s, "ABC", "string value"); 88 T_ASSERT_EQ_INT(s[4], 'e', "string value wasn't overwritten"); 89 } 90 91 T_DECL(scanf_decimal, "decimal") 92 { 93 int num; 94 for (char i = 0; i <= 9; ++i) { 95 char buf[] = { i + '0', '\0' }; 96 T_ASSERT_EQ_INT(sscanf(buf, "%d", &num), 1, "decimal matched"); 97 T_ASSERT_EQ_INT(num, i, "decimal value"); 98 } 99 for (char i = 10; i <= 99; ++i) { 100 char buf[] = { i / 10 + '0', i % 10 + '0', '\0' }; 101 T_ASSERT_EQ_INT(sscanf(buf, "%d", &num), 1, "decimal matched"); 102 T_ASSERT_EQ_INT(num, i, "decimal value"); 103 } 104 for (char i = 0; i <= 9; ++i) { 105 char buf[] = { '-', i + '0', '\0' }; 106 T_ASSERT_EQ_INT(sscanf(buf, "%d", &num), 1, "negative decimal matched"); 107 T_ASSERT_EQ_INT(num, -i, "negative decimal value"); 108 } 109 T_ASSERT_EQ_INT(sscanf("-2147483648", "%d", &num), 1, "INT32_MIN matched"); 110 T_ASSERT_EQ_INT(num, INT32_MIN, "INT32_MIN value"); 111 T_ASSERT_EQ_INT(sscanf("2147483647", "%d", &num), 1, "INT32_MAX matched"); 112 T_ASSERT_EQ_INT(num, INT32_MAX, "INT32_MAX value"); 113 } 114 115 T_DECL(scanf_integer, "integer") 116 { 117 int num; 118 T_ASSERT_EQ_INT(sscanf("0", "%i", &num), 1, "octal integer matched"); 119 T_ASSERT_EQ_INT(num, 0, "octal integer value"); 120 for (char i = 0; i <= 7; ++i) { 121 char buf[] = { '0', i + '0', '\0' }; 122 T_ASSERT_EQ_INT(sscanf(buf, "%i", &num), 1, "octal integer matched"); 123 T_ASSERT_EQ_INT(num, i, "octal integer value"); 124 } 125 for (char i = 0; i <= 9; ++i) { 126 char buf[] = { '0', 'x', i + '0', '\0' }; 127 T_ASSERT_EQ_INT(sscanf(buf, "%i", &num), 1, "hex integer matched"); 128 T_ASSERT_EQ_INT(num, i, "hex integer value"); 129 } 130 for (char i = 10; i <= 15; ++i) { 131 char buf[] = { '0', 'x', i - 10 + 'a', '\0' }; 132 T_ASSERT_EQ_INT(sscanf(buf, "%i", &num), 1, "hex integer matched"); 133 T_ASSERT_EQ_INT(num, i, "hex integer value"); 134 } 135 } 136 137 T_DECL(scanf_unsigned, "unsigned") 138 { 139 unsigned num; 140 T_ASSERT_EQ_INT(sscanf("4294967295", "%u", &num), 1, "UINT32_MAX matched"); 141 T_ASSERT_EQ_UINT(num, UINT32_MAX, "UINT32_MAX value"); 142 } 143 144 T_DECL(scanf_octal, "octal") 145 { 146 int num; 147 T_ASSERT_EQ_INT(sscanf("0", "%o", &num), 1, "octal matched"); 148 T_ASSERT_EQ_INT(num, 0, "octal value"); 149 for (char i = 0; i <= 7; ++i) { 150 char buf[] = { '0', i + '0', '\0' }; 151 T_ASSERT_EQ_INT(sscanf(buf, "%o", &num), 1, "octal matched"); 152 T_ASSERT_EQ_INT(num, i, "octal value"); 153 } 154 } 155 156 T_DECL(scanf_hex, "hex") 157 { 158 int num; 159 for (char i = 0; i <= 9; ++i) { 160 char buf[] = { '0', 'x', i + '0', '\0' }; 161 T_ASSERT_EQ_INT(sscanf(buf, "%x", &num), 1, "hex matched"); 162 T_ASSERT_EQ_INT(num, i, "hex value"); 163 } 164 for (char i = 10; i <= 15; ++i) { 165 char buf[] = { '0', 'x', i - 10 + 'a', '\0' }; 166 T_ASSERT_EQ_INT(sscanf(buf, "%x", &num), 1, "hex matched"); 167 T_ASSERT_EQ_INT(num, i, "hex value"); 168 } 169 } 170 171 T_DECL(scanf_read, "read") 172 { 173 int val, num; 174 T_ASSERT_EQ_INT(sscanf("", "%n", &num), 0, "read matched"); 175 T_ASSERT_EQ_INT(num, 0, "read count"); 176 T_ASSERT_EQ_INT(sscanf("a", "a%n", &num), 0, "read matched"); 177 T_ASSERT_EQ_INT(num, 1, "read count"); 178 T_ASSERT_EQ_INT(sscanf("ab", "a%nb", &num), 0, "read matched"); 179 T_ASSERT_EQ_INT(num, 1, "read count"); 180 T_ASSERT_EQ_INT(sscanf("1234567", "%i%n", &val, &num), 1, "read matched"); 181 T_ASSERT_EQ_INT(val, 1234567, "read value"); 182 T_ASSERT_EQ_INT(num, 7, "read count"); 183 } 184 185 T_DECL(scanf_pointer, "pointer") 186 { 187 void *ptr; 188 if (sizeof(void*) == 4) { 189 T_ASSERT_EQ_INT(sscanf("0xdeadbeef", "%p", &ptr), 1, "pointer matched"); 190 T_ASSERT_EQ_PTR(ptr, (void*)0xdeadbeef, "pointer value"); 191 } else { 192 T_ASSERT_EQ_INT(sscanf("0xdeadbeefc0defefe", "%p", &ptr), 1, "pointer matched"); 193 T_ASSERT_EQ_PTR(ptr, (void*)0xdeadbeefc0defefe, "pointer value"); 194 } 195 } 196