Blame src/functional/sscanf.c

nsz 1be84a
#include <stdio.h>
nsz 1be84a
#include <string.h>
nsz 1be84a
#include <limits.h>
nsz 1be84a
#include "test.h"
nsz 1be84a
nsz 1be84a
#define TEST(r, f, x, m) ( \
nsz 1be84a
	((r) = (f)) == (x) || \
nsz 1be84a
	(error("%s failed (" m ")\n", #f, r, x), 0) )
nsz 1be84a
nsz 1be84a
#define TEST_S(s, x, m) ( \
nsz 1be84a
	!strcmp((s),(x)) || \
nsz 1be84a
	(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
nsz 1be84a
nsz 1be84a
#define TEST_F(x) ( \
nsz 1be84a
	TEST(i, sscanf(# x, "%lf", &d), 1, "got %d fields, expected %d"), \
nsz 1be84a
	TEST(t, d, (double)x, "%g != %g") )
nsz 1be84a
nsz 462b4f
int main(void)
nsz 462b4f
{
nsz 1be84a
	int i;
nsz 1be84a
	char a[100], b[100];
nsz 1be84a
	int x, y, z, u, v;
nsz 1be84a
	double d, t;
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf("hello, world\n", "%s %s", a, b), 2, "only %d fields, expected %d");
nsz 1be84a
	TEST_S(a, "hello,", "");
nsz 1be84a
	TEST_S(b, "world", "");
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf("hello, world\n", "%[hel]%s", a, b), 2, "only %d fields, expected %d");
nsz 1be84a
	TEST_S(a, "hell", "");
nsz 1be84a
	TEST_S(b, "o,", "");
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf("hello, world\n", "%[hel] %s", a, b), 2, "only %d fields, expected %d");
nsz 1be84a
	TEST_S(a, "hell", "");
nsz 1be84a
	TEST_S(b, "o,", "");
nsz 1be84a
nsz 1be84a
	a[8] = 'X';
nsz 1be84a
	a[9] = 0;
nsz 1be84a
	TEST(i, sscanf("hello, world\n", "%8c%8c", a, b), 1, "%d fields, expected %d");
nsz 1be84a
	TEST_S(a, "hello, wX", "");
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf("56789 0123 56a72", "%2d%d%*d %[0123456789]\n", &x, &y, a), 3, "only %d fields, expected %d");
nsz 1be84a
	TEST(i, x, 56, "%d != %d");
nsz 1be84a
	TEST(i, y, 789, "%d != %d");
nsz 1be84a
	TEST_S(a, "56", "");
nsz 1be84a
nsz 1be84a
	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");
nsz 1be84a
	TEST(i, x, 9, "%d != %d");
nsz 1be84a
	TEST(i, y, 256, "%d != %d");
nsz 1be84a
	TEST(i, z, 9, "%d != %d");
nsz 1be84a
	TEST(i, u, 256, "%d != %d");
nsz 1be84a
	TEST(i, v, 256, "%d != %d");
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf("20 xyz", "%d %d\n", &x, &y), 1, "only %d fields, expected %d");
nsz 1be84a
	TEST(i, x, 20, "%d != %d");
nsz 1be84a
Szabolcs Nagy 7cf875
	TEST(i, sscanf("xyz", "%d %d\n", &x, &y), 0, "got %d fields, expected no match (%d)");
nsz 1be84a
Szabolcs Nagy 7cf875
	TEST(i, sscanf("", "%d %d\n", &x, &y), -1, "got %d fields, expected input failure (%d)");
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf(" 12345 6", "%2d%d%d", &x, &y, &z), 3, "only %d fields, expected %d");
nsz 1be84a
	TEST(i, x, 12, "%d != %d");
nsz 1be84a
	TEST(i, y, 345, "%d != %d");
nsz 1be84a
	TEST(i, z, 6, "%d != %d");
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf(" 0x12 0x34", "%5i%2i", &x, &y), 1, "got %d fields, expected %d");
nsz 1be84a
	TEST(i, x, 0x12, "%d != %d");
nsz 1be84a
nsz 1be84a
	TEST_F(123);
nsz 1be84a
	TEST_F(123.0);
nsz 1be84a
	TEST_F(123.0e+0);
nsz 1be84a
	TEST_F(123.0e+4);
nsz 1be84a
	TEST_F(1.234e1234);
nsz 1be84a
	TEST_F(1.234e-1234);
nsz 1be84a
	TEST_F(1.234e56789);
nsz 1be84a
	TEST_F(1.234e-56789);
nsz 1be84a
	TEST_F(-0.5);
nsz 1be84a
	TEST_F(0.1);
nsz 1be84a
	TEST_F(0.2);
nsz 1be84a
	TEST_F(0.1e-10);
nsz 1be84a
	TEST_F(0x1234p56);
nsz 1be84a
nsz 1be84a
	TEST(i, sscanf("10e", "%lf", &d), 0, "got %d fields, expected no match (%d)");
nsz 1be84a
	TEST(i, sscanf("", "%lf\n", &d), -1, "got %d fields, expected input failure (%d)");
nsz 462b4f
	return test_status;
nsz 1be84a
}