/*-1* Copyright (c) 2011 Giorgos Keramidas. All rights reserved.2* Copyright (c) 2007 Diomidis Spinellis. All rights reserved.3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425#include <sys/types.h>2627#include <assert.h>28#include <errno.h>29#include <float.h>30#include <limits.h>31#include <math.h>32#include <stdio.h>33#include <stdint.h>34#include <stdlib.h>35#include <strings.h>36#include <syslog.h>37#include <time.h>3839#include <atf-c.h>4041#define KASSERT(val, msg) assert(val)4243typedef u_int32_t comp_t;4445#define AHZ 10000004647#include "convert.c"4849union cf {50comp_t c;51float f;52};5354static void55check_result(const char *name, float expected, union cf v)56{57double eps;5859eps = fabs(expected - v.f) / expected;60ATF_CHECK(eps <= FLT_EPSILON);61if (eps > FLT_EPSILON) {62printf("Error in %s\n", name);63printf("Got 0x%08x %12g\n", v.c, v.f);64v.f = expected;65printf("Expected 0x%08x %12g (%.15lg)\n", v.c, v.f, expected);66printf("Epsilon=%lg, rather than %g\n", eps, FLT_EPSILON);67}68}6970/*71* Test case for encoding {0 sec, 0 usec} within a reasonable epsilon.72*/7374ATF_TC_WITHOUT_HEAD(encode_tv_zero);75ATF_TC_BODY(encode_tv_zero, tc)76{77union cf v;78struct timeval tv;7980tv.tv_sec = 0;81tv.tv_usec = 0;82v.c = encode_timeval(tv);83ATF_CHECK(fabs(v.f - 0.0) < FLT_EPSILON);84}8586/*87* Test case for encoding a random long number.88*/8990ATF_TC_WITHOUT_HEAD(encode_long);91ATF_TC_BODY(encode_long, tc)92{93union cf v;94long l;9596l = random();97v.c = encode_long(l);98check_result(atf_tc_get_ident(tc), l, v);99}100101/*102* Test case for encoding a small number of seconds {1 sec, 0 usec}.103*/104105ATF_TC_WITHOUT_HEAD(encode_tv_only_sec);106ATF_TC_BODY(encode_tv_only_sec, tc)107{108union cf v;109struct timeval tv;110111tv.tv_sec = 1;112tv.tv_usec = 0;113v.c = encode_timeval(tv);114check_result(atf_tc_get_ident(tc),115(float)tv.tv_sec * AHZ + tv.tv_usec, v);116}117118/*119* Test case for encoding a small number of usec {0 sec, 1 usec}.120*/121122ATF_TC_WITHOUT_HEAD(encode_tv_only_usec);123ATF_TC_BODY(encode_tv_only_usec, tc)124{125union cf v;126struct timeval tv;127128tv.tv_sec = 0;129tv.tv_usec = 1;130v.c = encode_timeval(tv);131check_result(atf_tc_get_ident(tc),132(float)tv.tv_sec * AHZ + tv.tv_usec, v);133}134135/*136* Test case for encoding a large number of usec {1 sec, 999.999 usec}.137*/138139ATF_TC_WITHOUT_HEAD(encode_tv_many_usec);140ATF_TC_BODY(encode_tv_many_usec, tc)141{142union cf v;143struct timeval tv;144145tv.tv_sec = 1;146tv.tv_usec = 999999L;147v.c = encode_timeval(tv);148check_result(atf_tc_get_ident(tc),149(float)tv.tv_sec * AHZ + tv.tv_usec, v);150}151152/*153* Test case for encoding a huge number of usec {1 sec, 1.000.000 usec} that154* overflows the usec counter and should show up as an increase in timeval's155* seconds instead.156*/157158ATF_TC_WITHOUT_HEAD(encode_tv_usec_overflow);159ATF_TC_BODY(encode_tv_usec_overflow, tc)160{161union cf v;162struct timeval tv;163164tv.tv_sec = 1;165tv.tv_usec = 1000000L;166v.c = encode_timeval(tv);167check_result(atf_tc_get_ident(tc),168(float)tv.tv_sec * AHZ + tv.tv_usec, v);169}170171/*172* Test case for encoding a very large number of seconds, one that is very173* near to the limit of 32-bit signed values. With a usec value of 999.999174* microseconds this should result in the largest value we can represent with175* a timeval struct.176*/177178ATF_TC_WITHOUT_HEAD(encode_tv_upper_limit);179ATF_TC_BODY(encode_tv_upper_limit, tc)180{181union cf v;182struct timeval tv;183184tv.tv_sec = 2147483647L;185tv.tv_usec = 999999L;186v.c = encode_timeval(tv);187check_result(atf_tc_get_ident(tc),188(float)tv.tv_sec * AHZ + tv.tv_usec, v);189}190191/*192* Test case for encoding a million random timeval objects, and checking that193* the conversion does not diverge too much from the expected values.194*/195196ATF_TC_WITHOUT_HEAD(encode_tv_random_million);197ATF_TC_BODY(encode_tv_random_million, tc)198{199union cf v;200struct timeval tv;201long k;202203#ifdef __LP64__204atf_tc_expect_fail("the testcase violates FLT_EPSILON on 64-bit "205"platforms, e.g. amd64");206#endif207208ATF_REQUIRE_MSG(unsetenv("TZ") == 0, "unsetting TZ failed; errno=%d", errno);209210for (k = 1; k < 1000000L; k++) {211tv.tv_sec = random();212tv.tv_usec = (random() % 1000000L);213v.c = encode_timeval(tv);214check_result(atf_tc_get_ident(tc),215(float)tv.tv_sec * AHZ + tv.tv_usec, v);216}217}218219/* ---------------------------------------------------------------------220* Main.221* --------------------------------------------------------------------- */222223ATF_TP_ADD_TCS(tp)224{225226ATF_TP_ADD_TC(tp, encode_long);227ATF_TP_ADD_TC(tp, encode_tv_zero);228ATF_TP_ADD_TC(tp, encode_tv_only_sec);229ATF_TP_ADD_TC(tp, encode_tv_only_usec);230ATF_TP_ADD_TC(tp, encode_tv_many_usec);231ATF_TP_ADD_TC(tp, encode_tv_usec_overflow);232ATF_TP_ADD_TC(tp, encode_tv_upper_limit);233ATF_TP_ADD_TC(tp, encode_tv_random_million);234235return atf_no_error();236}237238239