public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Add tests for abs(), labs(), llabs() (BZ #30263)
@ 2023-04-03 19:12 Joe Simmons-Talbott
  2023-04-03 19:12 ` [PATCH 1/3] stdlib: Add testcases for abs(). " Joe Simmons-Talbott
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Joe Simmons-Talbott @ 2023-04-03 19:12 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Add testcases for abs(), labs(), and llabs().  Test the bounds of the
given type, zero, and part of the full range of values for labs() and
llabs().  Test the full range for abs().

Joe Simmons-Talbott (3):
  stdlib: Add testcases for abs(). (BZ #30263)
  stdlib: Add testcases for labs(). (BZ #30263)
  stdlib: Add testcases for llabs(). (BZ #30263)

 stdlib/Makefile    |  7 +++++++
 stdlib/tst-abs.c   | 43 ++++++++++++++++++++++++++++++++++++++++
 stdlib/tst-labs.c  | 49 ++++++++++++++++++++++++++++++++++++++++++++++
 stdlib/tst-llabs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 148 insertions(+)
 create mode 100644 stdlib/tst-abs.c
 create mode 100644 stdlib/tst-labs.c
 create mode 100644 stdlib/tst-llabs.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] stdlib: Add testcases for abs(). (BZ #30263)
  2023-04-03 19:12 [PATCH 0/3] Add tests for abs(), labs(), llabs() (BZ #30263) Joe Simmons-Talbott
@ 2023-04-03 19:12 ` Joe Simmons-Talbott
  2023-04-03 19:12 ` [PATCH 2/3] stdlib: Add testcases for labs(). " Joe Simmons-Talbott
  2023-04-03 19:12 ` [PATCH 3/3] stdlib: Add testcases for llabs(). " Joe Simmons-Talbott
  2 siblings, 0 replies; 5+ messages in thread
From: Joe Simmons-Talbott @ 2023-04-03 19:12 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Test minimum and maximum int values, zero, and a good part of the range
of int values.  Use '-fno-builtin' to ensure we are testing the
implementation.
---
 stdlib/Makefile  |  3 +++
 stdlib/tst-abs.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 stdlib/tst-abs.c

diff --git a/stdlib/Makefile b/stdlib/Makefile
index 5e38f0e6a2..b7e1c028f0 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -184,6 +184,7 @@ tests := \
   testmb2 \
   testrand \
   testsort \
+  tst-abs \
   tst-arc4random-fork \
   tst-arc4random-stats \
   tst-arc4random-thread \
@@ -287,6 +288,8 @@ LDLIBS-test-dlclose-exit-race = $(shared-thread-library)
 LDFLAGS-test-dlclose-exit-race = $(LDFLAGS-rdynamic)
 LDLIBS-test-dlclose-exit-race-helper.so = $(libsupport) $(shared-thread-library)
 
+CFLAGS-tst-abs.c += -fno-builtin
+
 ifeq ($(have-cxx-thread_local),yes)
 CFLAGS-tst-quick_exit.o = -std=c++11
 LDLIBS-tst-quick_exit = -lstdc++
diff --git a/stdlib/tst-abs.c b/stdlib/tst-abs.c
new file mode 100644
index 0000000000..639e29d33f
--- /dev/null
+++ b/stdlib/tst-abs.c
@@ -0,0 +1,43 @@
+/* Basic tests for abs.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <limits.h>
+#include <stdlib.h>
+
+#include <support/check.h>
+
+static int do_test (void)
+{
+  int i;
+
+  TEST_COMPARE(abs(INT_MAX), INT_MAX);
+  TEST_COMPARE(abs(INT_MIN + 1), INT_MAX);
+  TEST_COMPARE(abs(-1), 1);
+  TEST_COMPARE(abs(0), 0);
+  TEST_COMPARE(abs(1), 1);
+
+  for (i = INT_MIN + 1; i < 0; i++)
+    TEST_COMPARE(abs(i), -i);
+
+  for (i = 0; i < INT_MAX; i++)
+    TEST_COMPARE(abs(i), i);
+
+  return EXIT_SUCCESS;
+}
+
+#include <support/test-driver.c>
-- 
2.39.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] stdlib: Add testcases for labs(). (BZ #30263)
  2023-04-03 19:12 [PATCH 0/3] Add tests for abs(), labs(), llabs() (BZ #30263) Joe Simmons-Talbott
  2023-04-03 19:12 ` [PATCH 1/3] stdlib: Add testcases for abs(). " Joe Simmons-Talbott
@ 2023-04-03 19:12 ` Joe Simmons-Talbott
  2023-04-03 19:12 ` [PATCH 3/3] stdlib: Add testcases for llabs(). " Joe Simmons-Talbott
  2 siblings, 0 replies; 5+ messages in thread
From: Joe Simmons-Talbott @ 2023-04-03 19:12 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Test minimum and maximum long values, zero, and part of the range
of long values.  Use '-fno-builtin' to ensure we are testing the
implementation.
---
 stdlib/Makefile   |  2 ++
 stdlib/tst-labs.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 stdlib/tst-labs.c

diff --git a/stdlib/Makefile b/stdlib/Makefile
index b7e1c028f0..5899cc1865 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -198,6 +198,7 @@ tests := \
   tst-cxa_atexit \
   tst-environ \
   tst-getrandom \
+  tst-labs \
   tst-limits \
   tst-makecontext \
   tst-makecontext-align \
@@ -289,6 +290,7 @@ LDFLAGS-test-dlclose-exit-race = $(LDFLAGS-rdynamic)
 LDLIBS-test-dlclose-exit-race-helper.so = $(libsupport) $(shared-thread-library)
 
 CFLAGS-tst-abs.c += -fno-builtin
+CFLAGS-tst-labs.c += -fno-builtin
 
 ifeq ($(have-cxx-thread_local),yes)
 CFLAGS-tst-quick_exit.o = -std=c++11
diff --git a/stdlib/tst-labs.c b/stdlib/tst-labs.c
new file mode 100644
index 0000000000..38c0f7b007
--- /dev/null
+++ b/stdlib/tst-labs.c
@@ -0,0 +1,49 @@
+/* Basic tests for labs.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <limits.h>
+#include <stdlib.h>
+
+#include <support/check.h>
+
+static int do_test (void)
+{
+  long i;
+
+  TEST_COMPARE(labs(LONG_MAX), LONG_MAX);
+  TEST_COMPARE(labs(LONG_MIN + 1), LONG_MAX);
+  TEST_COMPARE(labs(-1), 1);
+  TEST_COMPARE(labs(0), 0);
+  TEST_COMPARE(labs(1), 1);
+
+  for (i = LONG_MIN + 1; i < LONG_MIN + INT_MAX; i++)
+    TEST_COMPARE(llabs(i), -i);
+
+  for (i = LONG_MAX - INT_MAX; i < LONG_MAX; i++)
+    TEST_COMPARE(llabs(i), i);
+
+  for (i = INT_MIN + 1; i < 0; i++)
+    TEST_COMPARE(labs(i), -i);
+
+  for (i = 0; i < INT_MAX; i++)
+    TEST_COMPARE(labs(i), i);
+
+  return EXIT_SUCCESS;
+}
+
+#include <support/test-driver.c>
-- 
2.39.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] stdlib: Add testcases for llabs(). (BZ #30263)
  2023-04-03 19:12 [PATCH 0/3] Add tests for abs(), labs(), llabs() (BZ #30263) Joe Simmons-Talbott
  2023-04-03 19:12 ` [PATCH 1/3] stdlib: Add testcases for abs(). " Joe Simmons-Talbott
  2023-04-03 19:12 ` [PATCH 2/3] stdlib: Add testcases for labs(). " Joe Simmons-Talbott
@ 2023-04-03 19:12 ` Joe Simmons-Talbott
  2 siblings, 0 replies; 5+ messages in thread
From: Joe Simmons-Talbott @ 2023-04-03 19:12 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joe Simmons-Talbott

Test minimum and maximum long long values, zero, and part of the range
of long long values.  Use '-fno-builtin' to ensure we are testing the
implementation.
---
 stdlib/Makefile    |  2 ++
 stdlib/tst-llabs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 stdlib/tst-llabs.c

diff --git a/stdlib/Makefile b/stdlib/Makefile
index 5899cc1865..40886dea11 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -200,6 +200,7 @@ tests := \
   tst-getrandom \
   tst-labs \
   tst-limits \
+  tst-llabs \
   tst-makecontext \
   tst-makecontext-align \
   tst-makecontext2 \
@@ -291,6 +292,7 @@ LDLIBS-test-dlclose-exit-race-helper.so = $(libsupport) $(shared-thread-library)
 
 CFLAGS-tst-abs.c += -fno-builtin
 CFLAGS-tst-labs.c += -fno-builtin
+CFLAGS-tst-llabs.c += -fno-builtin
 
 ifeq ($(have-cxx-thread_local),yes)
 CFLAGS-tst-quick_exit.o = -std=c++11
diff --git a/stdlib/tst-llabs.c b/stdlib/tst-llabs.c
new file mode 100644
index 0000000000..631f13baa3
--- /dev/null
+++ b/stdlib/tst-llabs.c
@@ -0,0 +1,49 @@
+/* Basic tests for llabs.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <limits.h>
+#include <stdlib.h>
+
+#include <support/check.h>
+
+static int do_test (void)
+{
+  long i;
+
+  TEST_COMPARE(llabs(LLONG_MAX), LLONG_MAX);
+  TEST_COMPARE(llabs(LLONG_MIN + 1), LLONG_MAX);
+  TEST_COMPARE(llabs(-1), 1);
+  TEST_COMPARE(llabs(0), 0);
+  TEST_COMPARE(llabs(1), 1);
+
+  for (i = LLONG_MIN + 1; i < LLONG_MIN + INT_MAX; i++)
+    TEST_COMPARE(llabs(i), -i);
+
+  for (i = LLONG_MAX - INT_MAX; i < LLONG_MAX; i++)
+    TEST_COMPARE(llabs(i), i);
+
+  for (i = INT_MIN + 1; i < 0; i++)
+    TEST_COMPARE(llabs(i), -i);
+
+  for (i = 0; i < INT_MAX; i++)
+    TEST_COMPARE(llabs(i), i);
+
+  return EXIT_SUCCESS;
+}
+
+#include <support/test-driver.c>
-- 
2.39.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/3] Add tests for abs(), labs(), llabs() (BZ #30263)
@ 2023-04-03 20:01 Wilco Dijkstra
  0 siblings, 0 replies; 5+ messages in thread
From: Wilco Dijkstra @ 2023-04-03 20:01 UTC (permalink / raw)
  To: josimmon; +Cc: 'GNU C Library'

Hi Joe,

> Add testcases for abs(), labs(), and llabs().  Test the bounds of the
> given type, zero, and part of the full range of values for labs() and
> llabs().  Test the full range for abs().

How long do these tests take on older, slower cores? You're running
over 20 billion tests in total - at say 10 cycles per test a 2GHz CPU
would take 100 seconds which is way overkill for these basic functions.

Try using a larger increment (eg. prime) so that you cover the range
and the test will run within a second on a slow core. And for llabs it
makes sense to add tests for the carry when the bottom 32 bits are
zero to the top 32 bits (think 32-bit targets here).

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-03 20:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-03 19:12 [PATCH 0/3] Add tests for abs(), labs(), llabs() (BZ #30263) Joe Simmons-Talbott
2023-04-03 19:12 ` [PATCH 1/3] stdlib: Add testcases for abs(). " Joe Simmons-Talbott
2023-04-03 19:12 ` [PATCH 2/3] stdlib: Add testcases for labs(). " Joe Simmons-Talbott
2023-04-03 19:12 ` [PATCH 3/3] stdlib: Add testcases for llabs(). " Joe Simmons-Talbott
2023-04-03 20:01 [PATCH 0/3] Add tests for abs(), labs(), llabs() " Wilco Dijkstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).