t_scanf.c revision 272345
112510Sdg/* $NetBSD: t_scanf.c,v 1.3 2012/03/18 07:00:51 jruoho Exp $ */
212510Sdg
312510Sdg/*-
412510Sdg * Copyright (c) 2010 The NetBSD Foundation, Inc.
512510Sdg * All rights reserved.
612510Sdg *
712510Sdg * Redistribution and use in source and binary forms, with or without
812510Sdg * modification, are permitted provided that the following conditions
912510Sdg * are met:
1012510Sdg * 1. Redistributions of source code must retain the above copyright
1112510Sdg *    notice, this list of conditions and the following disclaimer.
1212510Sdg * 2. Redistributions in binary form must reproduce the above copyright
1312510Sdg *    notice, this list of conditions and the following disclaimer in the
1412510Sdg *    documentation and/or other materials provided with the distribution.
1512510Sdg *
1612510Sdg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1712510Sdg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1812510Sdg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1912510Sdg * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2012510Sdg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2112510Sdg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2212510Sdg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2312510Sdg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2412510Sdg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2512510Sdg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2612510Sdg * POSSIBILITY OF SUCH DAMAGE.
2750477Speter */
2812510Sdg
2912510Sdg#include <atf-c.h>
3012510Sdg#include <math.h>
3112510Sdg#include <stdio.h>
3212510Sdg#include <string.h>
3312510Sdg
3412510Sdg#define NUM     -0x1234
3512510Sdg#define STRNUM  ___STRING(NUM)
3629138Sdg
3729138SdgATF_TC(sscanf_neghex);
3829138SdgATF_TC_HEAD(sscanf_neghex, tc)
3929138Sdg{
4029138Sdg	atf_tc_set_md_var(tc, "descr",
4129138Sdg	    "PR lib/21691: %%i and %%x fail with negative hex numbers");
4229138Sdg}
4329138Sdg
4429138SdgATF_TC_BODY(sscanf_neghex, tc)
4529138Sdg{
4629138Sdg        int i;
4729138Sdg
4812510Sdg        sscanf(STRNUM, "%i", &i);
4929138Sdg	ATF_REQUIRE(i == NUM);
5029138Sdg
5129138Sdg        sscanf(STRNUM, "%x", &i);
5229138Sdg	ATF_REQUIRE(i == NUM);
5329138Sdg}
5429138Sdg
5529138SdgATF_TC(sscanf_whitespace);
5629138SdgATF_TC_HEAD(sscanf_whitespace, tc)
5722255Sdg{
5822255Sdg
5922255Sdg	atf_tc_set_md_var(tc, "descr", "verify sscanf skips all whitespace");
6022255Sdg}
6122255Sdg
6212510SdgATF_TC_BODY(sscanf_whitespace, tc)
6312510Sdg{
6412510Sdg	const char str[] = "\f\n\r\t\v%z";
6512510Sdg	char c;
6612510Sdg
6712510Sdg        /* set of "white space" symbols from isspace(3) */
6812510Sdg        c = 0;
6912510Sdg        (void)sscanf(str, "%%%c", &c);
7012510Sdg	ATF_REQUIRE(c == 'z');
7112510Sdg}
7212510Sdg
7312510Sdg
7412510SdgATF_TP_ADD_TCS(tp)
7512510Sdg{
7612510Sdg
7712510Sdg	ATF_TP_ADD_TC(tp, sscanf_neghex);
7812510Sdg	ATF_TP_ADD_TC(tp, sscanf_whitespace);
7912510Sdg
8012510Sdg	return atf_no_error();
8112510Sdg}
8212510Sdg