1570af302Sopenharmony_ci#include <stdio.h> 2570af302Sopenharmony_ci#include <string.h> 3570af302Sopenharmony_ci#include <limits.h> 4570af302Sopenharmony_ci#include "test.h" 5570af302Sopenharmony_ci 6570af302Sopenharmony_ci#define TEST(r, f, x, m) ( \ 7570af302Sopenharmony_ci ((r) = (f)) == (x) || \ 8570af302Sopenharmony_ci (t_error("%s failed (" m ")\n", #f, r, x), 0) ) 9570af302Sopenharmony_ci 10570af302Sopenharmony_ci#define TEST_S(s, x, m) ( \ 11570af302Sopenharmony_ci !strcmp((s),(x)) || \ 12570af302Sopenharmony_ci (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) 13570af302Sopenharmony_ci 14570af302Sopenharmony_ci#define TEST_F(x) ( \ 15570af302Sopenharmony_ci TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \ 16570af302Sopenharmony_ci TEST(t, d, (double)x, "%g != %g") ) 17570af302Sopenharmony_ci 18570af302Sopenharmony_ciint main(void) 19570af302Sopenharmony_ci{ 20570af302Sopenharmony_ci int i; 21570af302Sopenharmony_ci char a[100], b[100]; 22570af302Sopenharmony_ci int x, y, z, u, v; 23570af302Sopenharmony_ci double d, t; 24570af302Sopenharmony_ci 25570af302Sopenharmony_ci TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d"); 26570af302Sopenharmony_ci TEST_S(a, "hello,", ""); 27570af302Sopenharmony_ci TEST_S(b, "world", ""); 28570af302Sopenharmony_ci 29570af302Sopenharmony_ci TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d"); 30570af302Sopenharmony_ci TEST_S(a, "hell", ""); 31570af302Sopenharmony_ci TEST_S(b, "o,", ""); 32570af302Sopenharmony_ci 33570af302Sopenharmony_ci TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d"); 34570af302Sopenharmony_ci TEST_S(a, "hell", ""); 35570af302Sopenharmony_ci TEST_S(b, "o,", ""); 36570af302Sopenharmony_ci 37570af302Sopenharmony_ci a[8] = 'X'; 38570af302Sopenharmony_ci a[9] = 0; 39570af302Sopenharmony_ci TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d"); 40570af302Sopenharmony_ci TEST_S(a, "hello, wX", ""); 41570af302Sopenharmony_ci 42570af302Sopenharmony_ci TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d"); 43570af302Sopenharmony_ci TEST(i, x, 56, "%d != %d"); 44570af302Sopenharmony_ci TEST(i, y, 789, "%d != %d"); 45570af302Sopenharmony_ci TEST_S(a, "56", ""); 46570af302Sopenharmony_ci 47570af302Sopenharmony_ci TEST(i, sscanf("011 0x100 11 0x100 100", "%i %i %o %x %x\n", &x, &y, &z, &u, &v), 5, "only %d fields, expected %d"); 48570af302Sopenharmony_ci TEST(i, x, 9, "%d != %d"); 49570af302Sopenharmony_ci TEST(i, y, 256, "%d != %d"); 50570af302Sopenharmony_ci TEST(i, z, 9, "%d != %d"); 51570af302Sopenharmony_ci TEST(i, u, 256, "%d != %d"); 52570af302Sopenharmony_ci TEST(i, v, 256, "%d != %d"); 53570af302Sopenharmony_ci 54570af302Sopenharmony_ci TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d"); 55570af302Sopenharmony_ci TEST(i, x, 20, "%d != %d"); 56570af302Sopenharmony_ci 57570af302Sopenharmony_ci TEST(i, sscanf("xyz", "%d %d\n", &x, &y), 0, "got %d fields, expected no match (%d)"); 58570af302Sopenharmony_ci 59570af302Sopenharmony_ci TEST(i, sscanf("", "%d %d\n", &x, &y), -1, "got %d fields, expected input failure (%d)"); 60570af302Sopenharmony_ci 61570af302Sopenharmony_ci TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d"); 62570af302Sopenharmony_ci TEST(i, x, 12, "%d != %d"); 63570af302Sopenharmony_ci TEST(i, y, 345, "%d != %d"); 64570af302Sopenharmony_ci TEST(i, z, 6, "%d != %d"); 65570af302Sopenharmony_ci 66570af302Sopenharmony_ci TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d"); 67570af302Sopenharmony_ci TEST(i, x, 0x12, "%d != %d"); 68570af302Sopenharmony_ci 69570af302Sopenharmony_ci TEST_F(123); 70570af302Sopenharmony_ci TEST_F(123.0); 71570af302Sopenharmony_ci TEST_F(123.0e+0); 72570af302Sopenharmony_ci TEST_F(123.0e+4); 73570af302Sopenharmony_ci TEST_F(1.234e1234); 74570af302Sopenharmony_ci TEST_F(1.234e-1234); 75570af302Sopenharmony_ci TEST_F(1.234e56789); 76570af302Sopenharmony_ci TEST_F(1.234e-56789); 77570af302Sopenharmony_ci TEST_F(-0.5); 78570af302Sopenharmony_ci TEST_F(0.1); 79570af302Sopenharmony_ci TEST_F(0.2); 80570af302Sopenharmony_ci TEST_F(0.1e-10); 81570af302Sopenharmony_ci TEST_F(0x1234p56); 82570af302Sopenharmony_ci 83570af302Sopenharmony_ci TEST(i, sscanf("10e", "%lf", &d), 1, "got %d fields, expected no match (%d)"); 84570af302Sopenharmony_ci TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)"); 85570af302Sopenharmony_ci return t_status; 86570af302Sopenharmony_ci} 87