t_scanf.c revision 1.1
11.1Sjruoho/* $NetBSD: t_scanf.c,v 1.1 2011/07/08 06:38:04 jruoho Exp $ */
21.1Sjruoho
31.1Sjruoho/*-
41.1Sjruoho * Copyright (c) 2010 The NetBSD Foundation, Inc.
51.1Sjruoho * All rights reserved.
61.1Sjruoho *
71.1Sjruoho * Redistribution and use in source and binary forms, with or without
81.1Sjruoho * modification, are permitted provided that the following conditions
91.1Sjruoho * are met:
101.1Sjruoho * 1. Redistributions of source code must retain the above copyright
111.1Sjruoho *    notice, this list of conditions and the following disclaimer.
121.1Sjruoho * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjruoho *    notice, this list of conditions and the following disclaimer in the
141.1Sjruoho *    documentation and/or other materials provided with the distribution.
151.1Sjruoho *
161.1Sjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sjruoho * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sjruoho * POSSIBILITY OF SUCH DAMAGE.
271.1Sjruoho */
281.1Sjruoho
291.1Sjruoho#include <atf-c.h>
301.1Sjruoho#include <math.h>
311.1Sjruoho#include <stdio.h>
321.1Sjruoho#include <string.h>
331.1Sjruoho
341.1Sjruoho#define NUM     -0x1234
351.1Sjruoho#define STRNUM  ___STRING(NUM)
361.1Sjruoho
371.1SjruohoATF_TC(sscanf_neghex);
381.1SjruohoATF_TC_HEAD(sscanf_neghex, tc)
391.1Sjruoho{
401.1Sjruoho
411.1Sjruoho	atf_tc_set_md_var(tc, "descr", \
421.1Sjruoho	    "PR lib/21691: %i and %x fail with negative hex numbers");
431.1Sjruoho}
441.1Sjruoho
451.1SjruohoATF_TC_BODY(sscanf_neghex, tc)
461.1Sjruoho{
471.1Sjruoho        int i;
481.1Sjruoho
491.1Sjruoho        sscanf(STRNUM, "%i", &i);
501.1Sjruoho	ATF_REQUIRE(i == NUM);
511.1Sjruoho
521.1Sjruoho        sscanf(STRNUM, "%x", &i);
531.1Sjruoho	ATF_REQUIRE(i == NUM);
541.1Sjruoho}
551.1Sjruoho
561.1SjruohoATF_TC(sscanf_whitespace);
571.1SjruohoATF_TC_HEAD(sscanf_whitespace, tc)
581.1Sjruoho{
591.1Sjruoho
601.1Sjruoho	atf_tc_set_md_var(tc, "descr", "verify sscanf skips all whitespace");
611.1Sjruoho}
621.1Sjruoho
631.1SjruohoATF_TC_BODY(sscanf_whitespace, tc)
641.1Sjruoho{
651.1Sjruoho	const char str[] = "\f\n\r\t\v%z";
661.1Sjruoho	char c;
671.1Sjruoho
681.1Sjruoho        /* set of "white space" symbols from isspace(3) */
691.1Sjruoho        c = 0;
701.1Sjruoho        (void)sscanf(str, "%%%c", &c);
711.1Sjruoho	ATF_REQUIRE(c == 'z');
721.1Sjruoho}
731.1Sjruoho
741.1Sjruoho
751.1SjruohoATF_TP_ADD_TCS(tp)
761.1Sjruoho{
771.1Sjruoho
781.1Sjruoho	ATF_TP_ADD_TC(tp, sscanf_neghex);
791.1Sjruoho	ATF_TP_ADD_TC(tp, sscanf_whitespace);
801.1Sjruoho
811.1Sjruoho	return atf_no_error();
821.1Sjruoho}
83