From patchwork Fri Oct 11 00:33:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1995845 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=KzegfPXL; 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 4XPnhS62qrz1xsc for ; Fri, 11 Oct 2024 11:35:04 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC376385734D for ; Fri, 11 Oct 2024 00:35:01 +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 6A473385735B for ; Fri, 11 Oct 2024 00:33:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6A473385735B 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 6A473385735B 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=1728606844; cv=none; b=thtOEzdEzFHaoEwBQFCLWHD2LWIWxytXuVbAL5asyaPLepHTsX8ERvUPVRNwss7iWQWyGGZl8zUBOzo/rEulq+rBUN0uW5PddUHK7976NB6ZoBwDmrVmF3nLLwH9X7b4oet0RBh1VXIeQhGo0n4x22JuF46kgGIyBAu2OnMGtcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728606844; c=relaxed/simple; bh=7LrxrIiTRsMr/2w4dgqH85seqvDk3V6OAQQeugEX8fg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=PSdXM5anktGS5kW32GCHWfEgS+Y3F3m9l6Y90zDkMFxIphNcrBPB+QLswaKazdPVxicYTsX+Hiap0k0cqkW8qPaL02kggJ1I8jt8xjg2DCKpfhk8R8etj1lVDDBs8qRDsR43rsAAj9EtSmmUtu6r1O+2gGajn7a14HZkwWDlAIs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728606832; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=WTqIz9MJcUkpn5icczaDpaiqPDxk2PJ6MiWfRUugBwk=; b=KzegfPXLaYl7izAtL5I2oGiD3aF+ObVKfLsInuZqAyuwAryG5udONHb8aYw/H+Qbzs2czU pZfNRISiyeqOhr6JA2KH8W6B9ZYYPQXEXiaqBBK3Pt55TwQNXGpgoOkSusDMqCtv8w9r7Z U0dn6CVFSHVeHJsyM243gQ/VV5hSNSg= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-37-k2XTMfMqP0KrnheNGyNjqA-1; Thu, 10 Oct 2024 20:33:47 -0400 X-MC-Unique: k2XTMfMqP0KrnheNGyNjqA-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-42cb479fab2so13040055e9.1 for ; Thu, 10 Oct 2024 17:33:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728606826; x=1729211626; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=WTqIz9MJcUkpn5icczaDpaiqPDxk2PJ6MiWfRUugBwk=; b=wEj2XtwVOrLU2GVDaPEY0Is6RCS/hNv94c4XRBjrmu3ruCZPcVVBGJKwBG9XtB0yS0 IpiPVhLAyWxohjxXMMPEgRqSR3S8eMmSfh/mBTKet3ByRbxD5o8dDh8iFwm4ddSi1lSA U/AmoqERqT7QHCRYZy2Ev2CIZuOa0CYV0hCczFOiMSdhn+RifLIbMALl/9Amp9PoouPQ gvkZxMQotHFpydC215MOnqmdBEeWw1qcKrDcnuafS1TG5qc+AuJrlxLF+r1rmGyMJvg5 5qDJyp1kMh4R8s6j7XjMvY9cwdG1cza8YFYGEKBOVoE6vX2OD7tk1wzktj7wd94NaWHV 1IqQ== X-Gm-Message-State: AOJu0YzSVASCQ5M4bTa4BCiXLmC5tQHwg5yd5mjki0CoZ4NAtVHw010b MMkprPaBFeCxJA642K9POQ2Va8ysCE3kSo2//vapLZEpad+mDEQvJLS3tQh+bE1Qc15OuI+JQeB a01X1FC6e1GF3c/bjsPTeZ3gzS5DgFwDCko8ixod9qP+nZpswnAQrZBwsvBtLEVaYL7bNYBBn9k 4XNJRz7GJaByC+lfrUcga15EUBDw97UdGgM3omyv3kSw== X-Received: by 2002:a05:6000:18d:b0:374:b9ca:f1e8 with SMTP id ffacd0b85a97d-37d481c2398mr4042709f8f.20.1728606825614; Thu, 10 Oct 2024 17:33:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGbrw14WXDQDKwPlOGYwzU0Ss05V5mOwpYPKUvlm3catcTGDhqJ2Q+Dyr2ZVx5SYYf5ZNuTg== X-Received: by 2002:a05:6000:18d:b0:374:b9ca:f1e8 with SMTP id ffacd0b85a97d-37d481c2398mr4042687f8f.20.1728606824824; Thu, 10 Oct 2024 17:33:44 -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 5b1f17b1804b1-430ccf1f776sm61169085e9.3.2024.10.10.17.33.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 17:33:43 -0700 (PDT) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1sz3aZ-0000000Gaty-3TTE for libc-alpha@sourceware.org; Fri, 11 Oct 2024 00:33:03 +0000 Date: Fri, 11 Oct 2024 00:33:03 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: Add tests of time, gettimeofday, clock_gettime Message-ID: <685b5889-5044-13eb-f93c-68e15b0cd467@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, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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 There are no tests specifically focused on the functions time, gettimeofday and clock_gettime, although there are some incidental uses in tests of other functions. Add tests specifically for these three functions. Tested for x86_64 and x86. diff --git a/time/Makefile b/time/Makefile index d06797b06c..de22692683 100644 --- a/time/Makefile +++ b/time/Makefile @@ -53,6 +53,7 @@ tests := \ tst-adjtime \ tst-clock \ tst-clock2 \ + tst-clock_gettime \ tst-clock_nanosleep \ tst-clock_settime \ tst-cpuclock1 \ @@ -61,6 +62,7 @@ tests := \ tst-ftime \ tst-ftime_l \ tst-getdate \ + tst-gettimeofday \ tst-gmtime \ tst-itimer \ tst-mktime \ @@ -77,6 +79,7 @@ tests := \ tst-strptime-whitespace \ tst-strptime2 \ tst-strptime3 \ + tst-time \ tst-timegm \ tst-timespec_get \ tst-timespec_getres \ @@ -90,16 +93,19 @@ tests-time64 := \ tst-adjtime-time64 \ tst-clock-time64 \ tst-clock2-time64 \ + tst-clock_gettime-time64 \ tst-clock_nanosleep-time64 \ tst-clock_settime-time64 \ tst-cpuclock1-time64 \ tst-ctime-time64 \ tst-difftime-time64 \ + tst-gettimeofday-time64 \ tst-gmtime-time64 \ tst-itimer-time64 \ tst-mktime4-time64 \ tst-settimeofday-time64 \ tst-strftime4-time64 \ + tst-time-time64 \ tst-timegm-time64 \ tst-timespec_get-time64 \ tst-timespec_getres-time64 \ diff --git a/time/tst-clock_gettime-time64.c b/time/tst-clock_gettime-time64.c new file mode 100644 index 0000000000..5b215d11f8 --- /dev/null +++ b/time/tst-clock_gettime-time64.c @@ -0,0 +1 @@ +#include "tst-clock_gettime.c" diff --git a/time/tst-clock_gettime.c b/time/tst-clock_gettime.c new file mode 100644 index 0000000000..4bb92fc4b2 --- /dev/null +++ b/time/tst-clock_gettime.c @@ -0,0 +1,148 @@ +/* Test clock_gettime function. + Copyright (C) 2024 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 + . */ + +#include +#include +#include + +#include +#include + +/* Compare two struct timespec values, returning a value -1, 0 or 1. */ + +int +compare_timespec (const struct timespec *tv1, const struct timespec *tv2) +{ + if (tv1->tv_sec < tv2->tv_sec) + return -1; + if (tv1->tv_sec > tv2->tv_sec) + return 1; + if (tv1->tv_nsec < tv2->tv_nsec) + return -1; + if (tv1->tv_nsec > tv2->tv_nsec) + return 1; + return 0; +} + +struct test_clockid +{ + clockid_t clockid; + const char *name; + bool is_cputime; +}; + +#define CLOCK(clockid) { clockid, # clockid, false } +#define CLOCK_CPU(clockid) { clockid, # clockid, true } + +static const struct test_clockid clocks[] = + { + CLOCK (CLOCK_REALTIME), +#ifdef CLOCK_MONOTONIC + CLOCK (CLOCK_MONOTONIC), +#endif +#ifdef CLOCK_PROCESS_CPUTIME_ID + CLOCK_CPU (CLOCK_PROCESS_CPUTIME_ID), +#endif +#ifdef CLOCK_THREAD_CPUTIME_ID + CLOCK_CPU (CLOCK_THREAD_CPUTIME_ID), +#endif +#ifdef CLOCK_MONOTONIC_RAW + CLOCK (CLOCK_MONOTONIC_RAW), +#endif +#ifdef CLOCK_REALTIME_COARSE + CLOCK (CLOCK_REALTIME_COARSE), +#endif +#ifdef CLOCK_MONOTONIC_COARSE + CLOCK (CLOCK_MONOTONIC_COARSE), +#endif +#ifdef CLOCK_BOOTTIME + CLOCK (CLOCK_BOOTTIME), +#endif +#ifdef CLOCK_REALTIME_ALARM + CLOCK (CLOCK_REALTIME_ALARM), +#endif +#ifdef CLOCK_BOOTTIME_ALARM + CLOCK (CLOCK_BOOTTIME_ALARM), +#endif +#ifdef CLOCK_TAI + CLOCK (CLOCK_TAI), +#endif + }; + +int +do_test (void) +{ + /* Verify that the calls to clock_gettime succeed, that the time does + not decrease, and that time returns a truncated (not rounded) + version of the time. */ + for (size_t i = 0; i < sizeof clocks / sizeof clocks[0]; i++) + { + printf ("testing %s\n", clocks[i].name); + struct timespec ts1, ts2, ts3; + int ret; + time_t t1; + t1 = time (NULL); + TEST_VERIFY_EXIT (t1 != (time_t) -1); + ret = clock_gettime (clocks[i].clockid, &ts1); + TEST_VERIFY_EXIT (ret == 0); + if (clocks[i].clockid == CLOCK_REALTIME) + TEST_VERIFY (t1 <= ts1.tv_sec); + TEST_VERIFY (ts1.tv_nsec >= 0); + TEST_VERIFY (ts1.tv_nsec < 1000000000); + ret = clock_gettime (clocks[i].clockid, &ts2); + TEST_VERIFY_EXIT (ret == 0); + TEST_VERIFY (compare_timespec (&ts1, &ts2) <= 0); + TEST_VERIFY (ts2.tv_nsec >= 0); + TEST_VERIFY (ts2.tv_nsec < 1000000000); + /* Also verify that after sleeping, the time returned has + increased. Repeat several times to verify that each time, + the time from the time function is truncated not rounded. + For CPU time clocks, the time spent spinning on the CPU, and + so whether we end in the later half of a second, is not + predictable; thus, only test once for those clocks. */ + const struct timespec duration = { .tv_nsec = 100000000 }; + for (int j = 0; j < 5; j++) + { + if (clocks[i].is_cputime) + for (volatile unsigned int x = 0; x != (unsigned int) -1; x++) + ; + else + { + ret = nanosleep (&duration, NULL); + TEST_VERIFY_EXIT (ret == 0); + } + t1 = time (NULL); + TEST_VERIFY_EXIT (t1 != (time_t) -1); + ret = clock_gettime (clocks[i].clockid, &ts3); + TEST_VERIFY_EXIT (ret == 0); + TEST_VERIFY (compare_timespec (&ts2, &ts3) < 0); + if (clocks[i].clockid == CLOCK_REALTIME) + TEST_VERIFY (t1 <= ts3.tv_sec); + TEST_VERIFY (ts3.tv_nsec >= 0); + TEST_VERIFY (ts3.tv_nsec < 1000000000); + ts2 = ts3; + if (clocks[i].is_cputime) + break; + } + } + return 0; +} + +#define TIMEOUT 60 + +#include diff --git a/time/tst-gettimeofday-time64.c b/time/tst-gettimeofday-time64.c new file mode 100644 index 0000000000..6c08761ef9 --- /dev/null +++ b/time/tst-gettimeofday-time64.c @@ -0,0 +1 @@ +#include "tst-gettimeofday.c" diff --git a/time/tst-gettimeofday.c b/time/tst-gettimeofday.c new file mode 100644 index 0000000000..978ae28587 --- /dev/null +++ b/time/tst-gettimeofday.c @@ -0,0 +1,93 @@ +/* Test gettimeofday function. + Copyright (C) 2024 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 + . */ + +#include +#include + +#include +#include + +/* Compare two struct timeval values, returning a value -1, 0 or 1. */ + +int +compare_timeval (const struct timeval *tv1, const struct timeval *tv2) +{ + if (tv1->tv_sec < tv2->tv_sec) + return -1; + if (tv1->tv_sec > tv2->tv_sec) + return 1; + if (tv1->tv_usec < tv2->tv_usec) + return -1; + if (tv1->tv_usec > tv2->tv_usec) + return 1; + return 0; +} + +int +do_test (void) +{ + struct timeval tv1, tv2, tv3; + int ret; + time_t t1; + /* Verify that the calls to gettimeofday succeed, that the time does + not decrease, and that time returns a truncated (not rounded) + version of the time. */ + t1 = time (NULL); + TEST_VERIFY_EXIT (t1 != (time_t) -1); + ret = gettimeofday (&tv1, NULL); + TEST_VERIFY_EXIT (ret == 0); + TEST_VERIFY (t1 <= tv1.tv_sec); + TEST_VERIFY (tv1.tv_usec >= 0); + TEST_VERIFY (tv1.tv_usec < 1000000); + ret = gettimeofday (&tv2, NULL); + TEST_VERIFY_EXIT (ret == 0); + TEST_VERIFY (compare_timeval (&tv1, &tv2) <= 0); + TEST_VERIFY (tv2.tv_usec >= 0); + TEST_VERIFY (tv2.tv_usec < 1000000); + /* Also verify that after sleeping, the time returned has increased. + Repeat several times to verify that each time, the time from the + time function is truncated not rounded. */ + const struct timespec duration = { .tv_nsec = 100000000 }; + for (int i = 0; i < 10; i++) + { + ret = nanosleep (&duration, NULL); + TEST_VERIFY_EXIT (ret == 0); + t1 = time (NULL); + TEST_VERIFY_EXIT (t1 != (time_t) -1); + ret = gettimeofday (&tv3, NULL); + TEST_VERIFY_EXIT (ret == 0); + TEST_VERIFY (compare_timeval (&tv2, &tv3) < 0); + TEST_VERIFY (t1 <= tv3.tv_sec); + TEST_VERIFY (tv3.tv_usec >= 0); + TEST_VERIFY (tv3.tv_usec < 1000000); + tv2 = tv3; + } + /* Also test with the obsolete tz argument not being NULL. */ + struct timezone tz = { 0 }; + t1 = time (NULL); + TEST_VERIFY_EXIT (t1 != (time_t) -1); + ret = gettimeofday (&tv3, &tz); + TEST_VERIFY_EXIT (ret == 0); + TEST_VERIFY (t1 <= tv3.tv_sec); + TEST_VERIFY (compare_timeval (&tv2, &tv3) <= 0); + TEST_VERIFY (tv3.tv_usec >= 0); + TEST_VERIFY (tv3.tv_usec < 1000000); + return 0; +} + +#include diff --git a/time/tst-time-time64.c b/time/tst-time-time64.c new file mode 100644 index 0000000000..30e8d3c86e --- /dev/null +++ b/time/tst-time-time64.c @@ -0,0 +1 @@ +#include "tst-time.c" diff --git a/time/tst-time.c b/time/tst-time.c new file mode 100644 index 0000000000..7f24bed353 --- /dev/null +++ b/time/tst-time.c @@ -0,0 +1,51 @@ +/* Test time function. + Copyright (C) 2024 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 + . */ + +#include +#include + +#include +#include + +int +do_test (void) +{ + time_t t1, t2, t3, t4, t5, t6; + /* Verify that the calls to time succeed, that the value returned + directly equals that returned through the pointer passed, and + that the time does not decrease. */ + t1 = time (&t2); + TEST_VERIFY_EXIT (t1 != (time_t) -1); + TEST_VERIFY (t1 == t2); + t3 = time (NULL); + TEST_VERIFY_EXIT (t3 != (time_t) -1); + TEST_VERIFY (t3 >= t1); + /* Also verify that after sleeping, the time returned has + increased. */ + sleep (2); + t4 = time (&t5); + TEST_VERIFY_EXIT (t4 != (time_t) -1); + TEST_VERIFY (t4 == t5); + TEST_VERIFY (t4 > t3); + t6 = time (NULL); + TEST_VERIFY_EXIT (t6 != (time_t) -1); + TEST_VERIFY (t6 >= t4); + return 0; +} + +#include