Home | History | Annotate | Line # | Download | only in libutil
t_strpct.c revision 1.1
      1 /* $NetBSD: t_strpct.c,v 1.1 2025/05/02 19:52:02 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code was contributed to The NetBSD Foundation by Roland Illig.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __COPYRIGHT("@(#) Copyright (c) 2025\
     33  The NetBSD Foundation, inc. All rights reserved.");
     34 __RCSID("$NetBSD: t_strpct.c,v 1.1 2025/05/02 19:52:02 rillig Exp $");
     35 
     36 #include <stdbool.h>
     37 #include <stdint.h>
     38 #include <stdio.h>
     39 #include <string.h>
     40 #include <util.h>
     41 
     42 #include <atf-c.h>
     43 
     44 static void
     45 check_strspct(const char *file, unsigned line,
     46     size_t bufsiz, intmax_t num, intmax_t den, size_t digits,
     47     const char *want)
     48 {
     49 	char buf[128];
     50 
     51 	ATF_REQUIRE_MSG(bufsiz < sizeof(buf) - 2, "bufsiz too large");
     52 	memset(buf, '>', sizeof(buf));
     53 	buf[0] = '<';
     54 	buf[sizeof(buf) - 1] = '\0';
     55 
     56 	const char *have = strspct(buf + 1, bufsiz, num, den, digits);
     57 
     58 	ATF_REQUIRE_MSG(buf[0] == '<',
     59 	    "out-of-bounds write before");
     60 	ATF_REQUIRE_MSG(buf[1 + bufsiz] == '>',
     61 	    "out-of-bounds write after");
     62 	ATF_REQUIRE_MSG(have == buf + 1,
     63 	    "have != buf");
     64 	ATF_CHECK_MSG(bufsiz > 0 ? strcmp(have, want) == 0 : true,
     65 	    "%s:%u: want \"%s\", have \"%s\"",
     66 	    file, line, want, have);
     67 }
     68 
     69 #define h_strspct(bufsiz, num, den, digits, want) \
     70 	check_strspct(__FILE__, __LINE__, bufsiz, num, den, digits, want)
     71 
     72 ATF_TC(strspct);
     73 ATF_TC_HEAD(strspct, tc)
     74 {
     75 	atf_tc_set_md_var(tc, "descr", "Checks strspct(3)");
     76 }
     77 ATF_TC_BODY(strspct, tc)
     78 {
     79 
     80 	h_strspct(0, 0, 0, 0, "");
     81 	h_strspct(1, 0, 0, 0, "");
     82 	h_strspct(2, 0, 0, 0, "0");
     83 	h_strspct(3, 0, 0, 0, "0");
     84 	h_strspct(3, 0, 0, 1, "0.");
     85 	h_strspct(4, 0, 0, 1, "0.0");
     86 	h_strspct(4, 1, 5, 1, "20.");
     87 	h_strspct(6, 1, 5, 1, "20.0");
     88 	h_strspct(100, 1, 5, 5, "20.00000");
     89 
     90 	// Percentages are always rounded towards zero.
     91 	h_strspct(6, 1, 6, 1, "16.6");
     92 	h_strspct(7, -1, 6, 1, "-16.6");
     93 	h_strspct(7, 1, -6, 1, "-16.6");
     94 	h_strspct(7, -1, -6, 1, "16.6");
     95 
     96 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 0, "100");
     97 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 1, "100.0");
     98 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 2, "100.00");
     99 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 3, "100.000");
    100 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 4, "100.0000");
    101 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 5, "100.00000");
    102 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 6, "100.000000");
    103 	// FIXME: must be 100.0000000
    104 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 7, "100.0000003");
    105 	// FIXME: must be 100.00000000
    106 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 8, "100.00002208");
    107 	// FIXME: must be 100.000000000
    108 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 9, "100.000781031");
    109 	// FIXME: must be 100.0000000000
    110 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 10, "100.0040337943");
    111 	// FIXME: must be 100.00000000000
    112 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 11, "100.03657306783");
    113 	// FIXME: must be 100.000000000000
    114 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 12, "100.254043878856");
    115 	// FIXME: must be 100.0000000000000
    116 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 13, "102.4819115206086");
    117 	// FIXME: must be 100.00000000000000
    118 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 14, "92.23372036854775");
    119 	// FIXME: must be 100.000000000000000
    120 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 15, "9.223372036854775");
    121 	// FIXME: must be 100.0000000000000000
    122 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 16, "0.9223372036854775");
    123 	// FIXME: must be 100.00000000000000000
    124 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 17, "0.09223372036854775");
    125 	// FIXME: must be 100.000000000000000000
    126 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 18, "0.09223372036854775");
    127 	// FIXME: must be 100.0000000000000000000
    128 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 19, "0.09223372036854775");
    129 	// FIXME: must be 100.00000000000000000000
    130 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 20, "0.09223372036854775");
    131 	// FIXME: must be 100.000000000000000000000
    132 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 21, "0.09223372036854775");
    133 	// FIXME: must be 100.0000000000000000000000
    134 	h_strspct(100, INTMAX_MAX / 1000, INTMAX_MAX / 1000, 22, "0.09223372036854775");
    135 
    136 	// FIXME: must be 100.0
    137 	h_strspct(100, INTMAX_MIN, INTMAX_MIN, 20, "92.23372036854775808");
    138 	// FIXME: must be -100.0
    139 	h_strspct(100, INTMAX_MIN, INTMAX_MAX, 20, "-92.23372036854775808");
    140 	// FIXME: must be -100.0
    141 	h_strspct(100, INTMAX_MAX, INTMAX_MIN, 20, "-92.23372036854775807");
    142 	// FIXME: must be 100.0
    143 	h_strspct(100, INTMAX_MAX, INTMAX_MAX, 20, "92.23372036854775807");
    144 }
    145 
    146 ATF_TP_ADD_TCS(tp)
    147 {
    148 
    149 	ATF_TP_ADD_TC(tp, strspct);
    150 
    151 	return atf_no_error();
    152 }
    153