From patchwork Tue Aug 13 22:14:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1972119 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GqRmOmI+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wk5Ks51LYz1yfP for ; Wed, 14 Aug 2024 08:15:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DCC7B385841C for ; Tue, 13 Aug 2024 22:15:11 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id C13DE3858403 for ; Tue, 13 Aug 2024 22:14:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C13DE3858403 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C13DE3858403 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723587270; cv=none; b=AMmviBcJTW/+zGXCGJ10jJE4L4hdfUyTiLPWcVh/TA0NVvKdWhIG0SnLQjj7by2zpmDGpHsniiMgkGbaAvLBWEmEojdx3Xyp7qMTckM7QZTaamxgvxnWNQp0THQAehCyfaeBuNz/J17+rZYr0SY6DcRoUdJbFgmO5lDqblCxKag= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723587270; c=relaxed/simple; bh=aDksLuetkgyANxKM+zzmzNys21Uz8/g+XalcMF9F7eQ=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=RHocGdazQZubZYHEcySRjYxOyM/1tbzKxzood5qvVL1AyZBKwBV4K4A9475q06weMT/6mhxVrdrElOlHhNre4ZdRvbTXojg9bK7xbXwF6N4uH3ikPAMIU+GqSJZk6BeFzhVMUBvEyg26MjvVJmbjDai07Hb3SvbR6uW/ojlT6DY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723587268; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=2wKis8qWM05K1uk5vJBqzeow+uokxskWHsmGnQAWOkU=; b=GqRmOmI+m64IgYWF6c6sL0gW47iFBtGpPbVAW86Eue2KTP3RFShpiuYhIus6cpzN802xFu VOkvozLHFKhyeXRev9Xi0OAl4OkAqz1R4xJGQJcSv3YPp9cm3nlHnDO7xoMNCeAKj1/9Fd iXY6xjmY/c5cOxlybK7s2M3dwbXJNQE= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-623-B6RMjvvyODq4gUlbqPo6ug-1; Tue, 13 Aug 2024 18:14:27 -0400 X-MC-Unique: B6RMjvvyODq4gUlbqPo6ug-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4281310bf7aso42172475e9.1 for ; Tue, 13 Aug 2024 15:14:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723587266; x=1724192066; h=mime-version:references:message-id:in-reply-to:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2wKis8qWM05K1uk5vJBqzeow+uokxskWHsmGnQAWOkU=; b=L2q2E17xjEUMtzKq1+inCC52Ah7SEYer3nUWmWz3iD5v3ng+z+xf3OW0g7Fe/bM90I o7YaAkT38TWFii2mmuV7AS4Ry6UC5XiFIVPQwvHW0S24wx0ZB/GcXS+K012rayYVnlqU qQXamZtp9IlaIjckqoAPNkZ5126OX8scasMODV6B7C6URRyUUNIzNHD1sxhxL+BdvSBW nNff8PnXeS1AslQMkEgZEzfqPXfK6PYa0tPRCMt1U2PMhavH9BPXl6R9hukkXEzbSQbd l4NIs5TDkEc7/flWsyCsqAG2nJBJHMQEkC3qRoq9j0yHRYmX1LXLwfxDz/DGDp9jgvC3 6SHg== X-Gm-Message-State: AOJu0Yy3tgVz+WMSJzmyIxWZQHwmms3YaKE24bDIq+GcmXGhlA2b/aGR wxtYuJwfD4b/7+C3oQf/wNmfMYBle11zYaTjnZWN8d8FXgWJEAuzCS4ArCWK7W6rgFQvsDqQEer 91xvNxubdaW4bCq1KWnomFedjJDXpAC8cIe7ec0/Oq0q+NtLUcenlZNKJZqLzwklzUI2SXbJgsw Gt5IXJFRaqRFL+xZ4BhGFwOmE8zpazzz39wvH/4DjT6g== X-Received: by 2002:a5d:6291:0:b0:368:78ed:994c with SMTP id ffacd0b85a97d-371777645edmr633301f8f.17.1723587265752; Tue, 13 Aug 2024 15:14:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFfZ2YkgHIHe3bZQjwKhvA0DTcbMsgwCquK6RDCqLii9ZX2dbXCaD9xAW++nlwlP1hYK8fag== X-Received: by 2002:a5d:6291:0:b0:368:78ed:994c with SMTP id ffacd0b85a97d-371777645edmr633287f8f.17.1723587265045; Tue, 13 Aug 2024 15:14:25 -0700 (PDT) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36e4e51ec46sm11447450f8f.81.2024.08.13.15.14.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 15:14:24 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.95) (envelope-from ) id 1sdzmZ-0055iX-3C for libc-alpha@sourceware.org; Tue, 13 Aug 2024 22:14:23 +0000 Date: Tue, 13 Aug 2024 22:14:23 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: [PATCH v2] Make tst-strtod2 and tst-strtod5 type-generic In-Reply-To: <3b29db-e67c-e866-871f-4be525678d62@redhat.com> Message-ID: <82e5c8ec-c3fc-60db-6a6b-7ed22a56d67f@redhat.com> References: <3b29db-e67c-e866-871f-4be525678d62@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Make tst-strtod2 and tst-strtod5 type-generic Some of the strtod tests use type-generic machinery in tst-strtod.h to test the strto* functions for all floating types, while others only test double even when the tests are in fact meaningful for all floating types. Convert tst-strtod2 and tst-strtod5 to use the type-generic machinery so they test all floating types. I haven't tried to convert them to use newer test interfaces in other ways, just made the changes necessary to use the type-generic machinery. Tested for x86_64. Reviewed-by: Carlos O'Donell --- This is intended the be the first of several such patches improving strto* test coverage. For example, tst-strtod1i.c, tst-strtod3.c, tst-strtod4.c and tst-strtod5i.c can be changed similarly to cover all floating types, with some adjustments to take account of how they are actually testing an internal interface __strtod_internal. tst-strtod-underflow.c will be a more complicated case to adjust because each floating format needs its own test input strings. Changed in v2: one hardcoded "strtod" reference in a printf in cases of test failure was updated to reflect the function under test. diff --git a/stdlib/tst-strtod2.c b/stdlib/tst-strtod2.c index a7df82ebbd..2cb0953fa9 100644 --- a/stdlib/tst-strtod2.c +++ b/stdlib/tst-strtod2.c @@ -1,43 +1,61 @@ #include #include -struct test -{ - const char *str; - double result; - size_t offset; -} tests[] = -{ - { "0xy", 0.0, 1 }, - { "0x.y", 0.0, 1 }, - { "0x0.y", 0.0, 4 }, - { "0x.0y", 0.0, 4 }, - { ".y", 0.0, 0 }, - { "0.y", 0.0, 2 }, - { ".0y", 0.0, 2 } -}; +#include "tst-strtod.h" + +#define TEST_STRTOD(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \ +struct test_strto ## FSUF \ +{ \ + const char *str; \ + FTYPE result; \ + size_t offset; \ +} tests_strto ## FSUF[] = \ +{ \ + { "0xy", 0.0 ## LSUF, 1 }, \ + { "0x.y", 0.0 ## LSUF, 1 }, \ + { "0x0.y", 0.0 ## LSUF, 4 }, \ + { "0x.0y", 0.0 ## LSUF, 4 }, \ + { ".y", 0.0 ## LSUF, 0 }, \ + { "0.y", 0.0 ## LSUF, 2 }, \ + { ".0y", 0.0 ## LSUF, 2 } \ +}; \ + \ +static int \ +test_strto ## FSUF (void) \ +{ \ + int status = 0; \ + for (size_t i = 0; \ + i < sizeof (tests_strto ## FSUF) / sizeof (tests_strto ## FSUF[0]); \ + ++i) \ + { \ + char *ep; \ + FTYPE r = strto ## FSUF (tests_strto ## FSUF[i].str, &ep); \ + if (r != tests_strto ## FSUF[i].result) \ + { \ + char buf1[FSTRLENMAX], buf2[FSTRLENMAX]; \ + FTOSTR (buf1, sizeof (buf1), "%g", r); \ + FTOSTR (buf2, sizeof (buf2), "%g", tests_strto ## FSUF[i].result); \ + printf ("test %zu r = %s, expect %s\n", i, buf1, buf2); \ + status = 1; \ + } \ + if (ep != tests_strto ## FSUF[i].str + tests_strto ## FSUF[i].offset) \ + { \ + printf ("test %zu strto" #FSUF \ + " parsed %tu characters, expected %zu\n", \ + i, ep - tests_strto ## FSUF[i].str, \ + tests_strto ## FSUF[i].offset); \ + status = 1; \ + } \ + } \ + return status; \ +} + +GEN_TEST_STRTOD_FOREACH (TEST_STRTOD) static int do_test (void) { - int status = 0; - for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i) - { - char *ep; - double r = strtod (tests[i].str, &ep); - if (r != tests[i].result) - { - printf ("test %zu r = %g, expect %g\n", i, r, tests[i].result); - status = 1; - } - if (ep != tests[i].str + tests[i].offset) - { - printf ("test %zu strtod parsed %tu characters, expected %zu\n", - i, ep - tests[i].str, tests[i].offset); - status = 1; - } - } - return status; + return STRTOD_TEST_FOREACH (test_strto); } #define TEST_FUNCTION do_test () diff --git a/stdlib/tst-strtod5.c b/stdlib/tst-strtod5.c index 29153ec005..7eb9b3a2d7 100644 --- a/stdlib/tst-strtod5.c +++ b/stdlib/tst-strtod5.c @@ -22,35 +22,75 @@ #include #include +#include "tst-strtod.h" + #define NBSP "\xc2\xa0" -static const struct -{ - const char *in; - double expected; -} tests[] = - { - { "0", 0.0 }, - { "000", 0.0 }, - { "-0", -0.0 }, - { "-000", -0.0 }, - { "0,", 0.0 }, - { "-0,", -0.0 }, - { "0,0", 0.0 }, - { "-0,0", -0.0 }, - { "0e-10", 0.0 }, - { "-0e-10", -0.0 }, - { "0,e-10", 0.0 }, - { "-0,e-10", -0.0 }, - { "0,0e-10", 0.0 }, - { "-0,0e-10", -0.0 }, - { "0e-1000000", 0.0 }, - { "-0e-1000000", -0.0 }, - { "0,0e-1000000", 0.0 }, - { "-0,0e-1000000", -0.0 }, - }; -#define NTESTS (sizeof (tests) / sizeof (tests[0])) +#define TEST_STRTOD(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \ +static const struct \ +{ \ + const char *in; \ + FTYPE expected; \ +} tests_strto ## FSUF[] = \ + { \ + { "0", 0.0 ## LSUF }, \ + { "000", 0.0 ## LSUF }, \ + { "-0", -0.0 ## LSUF }, \ + { "-000", -0.0 ## LSUF }, \ + { "0,", 0.0 ## LSUF }, \ + { "-0,", -0.0 ## LSUF }, \ + { "0,0", 0.0 ## LSUF }, \ + { "-0,0", -0.0 ## LSUF }, \ + { "0e-10", 0.0 ## LSUF }, \ + { "-0e-10", -0.0 ## LSUF }, \ + { "0,e-10", 0.0 ## LSUF }, \ + { "-0,e-10", -0.0 ## LSUF }, \ + { "0,0e-10", 0.0 ## LSUF }, \ + { "-0,0e-10", -0.0 ## LSUF }, \ + { "0e-1000000", 0.0 ## LSUF }, \ + { "-0e-1000000", -0.0 ## LSUF }, \ + { "0,0e-1000000", 0.0 ## LSUF }, \ + { "-0,0e-1000000", -0.0 ## LSUF }, \ + }; \ + \ + \ +static int \ +test_strto ## FSUF (void) \ +{ \ + int status = 0; \ + \ + for (int i = 0; \ + i < sizeof (tests_strto ## FSUF) / sizeof (tests_strto ## FSUF[0]); \ + ++i) \ + { \ + char *ep; \ + FTYPE r = strto ## FSUF (tests_strto ## FSUF[i].in, &ep); \ + \ + if (*ep != '\0') \ + { \ + printf ("%d: got rest string \"%s\", expected \"\"\n", i, ep); \ + status = 1; \ + } \ + \ + if (r != tests_strto ## FSUF[i].expected \ + || (copysign ## CSUF (10.0 ## LSUF, r) \ + != copysign ## CSUF (10.0 ## LSUF, \ + tests_strto ## FSUF[i].expected))) \ + { \ + char buf1[FSTRLENMAX], buf2[FSTRLENMAX]; \ + FTOSTR (buf1, sizeof (buf1), "%g", r); \ + FTOSTR (buf2, sizeof (buf2), "%g", \ + tests_strto ## FSUF[i].expected); \ + printf ("%d: got wrong results %s, expected %s\n", \ + i, buf1, buf2); \ + status = 1; \ + } \ + } \ + \ + return status; \ +} +GEN_TEST_STRTOD_FOREACH (TEST_STRTOD) static int do_test (void) @@ -61,29 +101,7 @@ do_test (void) return 1; } - int status = 0; - - for (int i = 0; i < NTESTS; ++i) - { - char *ep; - double r = strtod (tests[i].in, &ep); - - if (*ep != '\0') - { - printf ("%d: got rest string \"%s\", expected \"\"\n", i, ep); - status = 1; - } - - if (r != tests[i].expected - || copysign (10.0, r) != copysign (10.0, tests[i].expected)) - { - printf ("%d: got wrong results %g, expected %g\n", - i, r, tests[i].expected); - status = 1; - } - } - - return status; + return STRTOD_TEST_FOREACH (test_strto); } #include