From patchwork Wed May 21 12:08:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 1048 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx21.g.dreamhost.com (peon2454.g.dreamhost.com [208.113.200.127]) by wilcox.dreamhost.com (Postfix) with ESMTP id 7B98F360079 for ; Wed, 21 May 2014 05:10:29 -0700 (PDT) Received: by homiemail-mx21.g.dreamhost.com (Postfix, from userid 14307373) id 1C3B9182E978; Wed, 21 May 2014 05:10:28 -0700 (PDT) X-Original-To: glibc@patchwork.siddhesh.in Delivered-To: x14307373@homiemail-mx21.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx21.g.dreamhost.com (Postfix) with ESMTPS id 95FED15C5160 for ; Wed, 21 May 2014 05:10:28 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; q=dns; s= default; b=EoVEtA/kK4vQIsdGMMC8fYpV32hot22aDSR24+8XZ01P9olDad+aL s+qCP1xTlqWHzRgxnIm1+AZ7LbL54nVB1HjPH3H4QJH8RQwY0VizpC9gGUkzvov2 DrFt3fwcT5wYEWeQU0IuMFyA27TktgwBrFJq235oswLeIqRoTrnFxw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; s=default; bh=mD+2nsSrCXvssDO884PNZ0ozJmk=; b=tt8tOGBv6MUWaNrZGkZ6xWtzyNBs bzBL+s/cgF93KaGTkAGwDHsBDmdMqaIRkIN7ZofaFV29NAZvVXifc9ghpeXKR/cG /hjk/ADR1cF2gKGakBjYwCJZ44eZ3ssROCHQRTSAdIhSKB4Ly51N9vS4muAqnjTj HUpzEDxQ0tsqTMQ= Received: (qmail 17309 invoked by alias); 21 May 2014 12:10:26 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 17299 invoked by uid 89); 21 May 2014 12:10:26 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-HELO: mail-la0-f43.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=t5ZAinVzRL9pwQwm9zWvgs8emuXweXCyoJCdXT/Oml8=; b=KqYfoNZGfr51msuUI2n4e/60NH8BJheg99BmXwmZpCebbQ6Y8OelcEfHBhmmoUW1m+ 8hqq/ifzu+mYyPjhm/H+Fv/SWSt1ZaNouPh9pOM5T/hqoMaTTP+/nr2wJAwXiiPdMMN/ a40MVxcbJJscp+OKpsm4KkkwqX81Y9MEnHqZ/oW0ZskifLLoP1/U870CFx/vKDJfILiH IKh2vOqW80N5OOxv9iTmuggVrlI+q84cQj6JzRkJgA3KO9+zBejPfJUfsXyDEUNTzggV 3QT9OYJlyx2BCLlHtX7JHGaO1/5hsqC1ngtet6w+ead39K73HPcWpTunh0ZUqKYjUOTS NKnw== X-Gm-Message-State: ALoCoQnW/Bi3DFuQlJM8x+4ppSyXtS6KXVkWBA7963wZuGBL+KnDZ70KaXlNrrUVBY5zn+M8EIn2 X-Received: by 10.112.131.8 with SMTP id oi8mr1108734lbb.87.1400674220976; Wed, 21 May 2014 05:10:20 -0700 (PDT) From: Rasmus Villemoes To: libc-alpha@sourceware.org Cc: Rasmus Villemoes Subject: [PATCH] sys/time.h: Remove comment about timercmp and add test Date: Wed, 21 May 2014 14:08:44 +0200 Message-Id: <1400674124-1789-1-git-send-email-rv@rasmusvillemoes.dk> X-DH-Original-To: glibc@patchwork.siddhesh.in The manual page timercmp(3) says "Some systems (but not Linux/glibc), have a broken timercmp() implementation, in which CMP of >=, <=, and == do not work". However, contains "NOTE: `timercmp' does not work for >= or <=.". Looking at the implementation of the macro, AFAICT the manual is correct. To be sure, I wrote a simple test program, which confirms this. Signed-off-by: Rasmus Villemoes --- time/Makefile | 2 +- time/sys/time.h | 3 +-- time/tst-timercmp.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 time/tst-timercmp.c diff --git a/time/Makefile b/time/Makefile index b7f3dba..20ddfb7 100644 --- a/time/Makefile +++ b/time/Makefile @@ -37,7 +37,7 @@ aux := era alt_digit lc-time-cleanup tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \ tst-getdate tst-mktime tst-mktime2 tst-ftime_l tst-strftime \ tst-mktime3 tst-strptime2 bug-asctime bug-asctime_r bug-mktime1 \ - tst-strptime3 bug-getdate1 tst-strptime-whitespace + tst-strptime3 bug-getdate1 tst-strptime-whitespace tst-timercmp include ../Rules diff --git a/time/sys/time.h b/time/sys/time.h index a3f219e..af3fed5 100644 --- a/time/sys/time.h +++ b/time/sys/time.h @@ -157,8 +157,7 @@ extern int futimesat (int __fd, const char *__file, #ifdef __USE_MISC -/* Convenience macros for operations on timevals. - NOTE: `timercmp' does not work for >= or <=. */ +/* Convenience macros for operations on timevals. */ # define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) # define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) # define timercmp(a, b, CMP) \ diff --git a/time/tst-timercmp.c b/time/tst-timercmp.c new file mode 100644 index 0000000..fcd9b17 --- /dev/null +++ b/time/tst-timercmp.c @@ -0,0 +1,74 @@ +#include +#include +#include + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + + +#define NTEST 100 +struct test { + struct timeval tv; + double d; +} test[NTEST]; + +/* + * We populate test[] with struct timevals with tv_sec in the range + * [-50,50]. This guarantees that the corresponding double values, + * though not able to represent tv_sec + tv_usec*1e-6 exactly, have + * sufficient granularity to distinguish different struct timevals. + */ +static void +prepare (void) +{ + for (int i = 0; i < NTEST; ++i) { + long s = (lrand48() % 101) - 50; + long us = (lrand48() % 1000000); + test[i].tv.tv_sec = s; + test[i].tv.tv_usec = us; + test[i].d = (double)s + 1e-6 * us; + } +} + +static void +fail(const struct test *a, const struct test *b, int x, int y, const char *cmp) +{ + printf("({.tv_sec = %ld, .tv_usec = %06ld} %s " + "{.tv_sec = %ld, .tv_usec = %06ld}) == %d, ", + (long)a->tv.tv_sec, (long)a->tv.tv_usec, cmp, + (long)b->tv.tv_sec, (long)b->tv.tv_usec, x); + printf("expected (%.6f %s %.6f) == %d\n", a->d, cmp, b->d, y); +} + +#define CHECK(a, b, cmp) do { \ + int x = !!(timercmp(&(a)->tv, &(b)->tv, cmp)); \ + int y = !!((a)->d cmp (b)->d); \ + if (x != y) { \ + fail(a, b, x, y, #cmp); \ + result = 1; \ + } \ + } while(0); + +static int +do_test(void) +{ + int result = 0; + for (int i = 0; i < NTEST; ++i) { + for (int j = i; j < NTEST; ++j) { + const struct test *a = &test[i]; + const struct test *b = &test[j]; + CHECK(a, b, ==); + CHECK(a, b, !=); + CHECK(a, b, <=); + CHECK(a, b, >=); + CHECK(a, b, <); + CHECK(a, b, >); + } + } + return result; +}