parse_time-test.c revision 233294
1251018Sgonzo/*
2251018Sgonzo * Copyright (c) 2004 Kungliga Tekniska H��gskolan
3251018Sgonzo * (Royal Institute of Technology, Stockholm, Sweden).
4251018Sgonzo * All rights reserved.
5251018Sgonzo *
6251018Sgonzo * Redistribution and use in source and binary forms, with or without
7251018Sgonzo * modification, are permitted provided that the following conditions
8251018Sgonzo * are met:
9251018Sgonzo *
10251018Sgonzo * 1. Redistributions of source code must retain the above copyright
11251018Sgonzo *    notice, this list of conditions and the following disclaimer.
12251018Sgonzo *
13251018Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
14251018Sgonzo *    notice, this list of conditions and the following disclaimer in the
15251018Sgonzo *    documentation and/or other materials provided with the distribution.
16251018Sgonzo *
17251018Sgonzo * 3. Neither the name of the Institute nor the names of its contributors
18251018Sgonzo *    may be used to endorse or promote products derived from this software
19251018Sgonzo *    without specific prior written permission.
20251018Sgonzo *
21251018Sgonzo * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22251018Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23251018Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24251018Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25251018Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26251018Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27251018Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28251018Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29251018Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30251018Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31251018Sgonzo * SUCH DAMAGE.
32251018Sgonzo */
33251018Sgonzo
34251018Sgonzo#include <config.h>
35251018Sgonzo
36251018Sgonzo#include "roken.h"
37251018Sgonzo#include "parse_time.h"
38251018Sgonzo#include "test-mem.h"
39251018Sgonzo#include "err.h"
40251018Sgonzo
41251018Sgonzostatic struct testcase {
42251018Sgonzo    size_t size;
43251018Sgonzo    int    val;
44251018Sgonzo    char  *str;
45251018Sgonzo} tests[] = {
46251018Sgonzo    { 8, 1,		"1 second" },
47251018Sgonzo    { 17, 61,		"1 minute 1 second" },
48251018Sgonzo    { 18, 62,		"1 minute 2 seconds" },
49251018Sgonzo    { 8, 60,		"1 minute" },
50251018Sgonzo    { 6, 3600,	 	"1 hour" },
51251018Sgonzo    { 15, 3601,	 	"1 hour 1 second" },
52251018Sgonzo    { 16, 3602,	 	"1 hour 2 seconds" }
53251018Sgonzo};
54251018Sgonzo
55251018Sgonzoint
56251018Sgonzomain(int argc, char **argv)
57251018Sgonzo{
58251018Sgonzo    size_t sz;
59251018Sgonzo    size_t buf_sz;
60251018Sgonzo    int i, j;
61251018Sgonzo
62251018Sgonzo    for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
63251018Sgonzo	char *buf;
64251018Sgonzo
65251018Sgonzo	sz = unparse_time(tests[i].val, NULL, 0);
66251018Sgonzo	if  (sz != tests[i].size)
67251018Sgonzo	    errx(1, "sz (%lu) != tests[%d].size (%lu)",
68251018Sgonzo		 (unsigned long)sz, i, (unsigned long)tests[i].size);
69251018Sgonzo
70251018Sgonzo	for (buf_sz = 0; buf_sz < tests[i].size + 2; buf_sz++) {
71251018Sgonzo
72251018Sgonzo	    buf = rk_test_mem_alloc(RK_TM_OVERRUN, "overrun",
73251018Sgonzo				    NULL, buf_sz);
74251018Sgonzo	    sz = unparse_time(tests[i].val, buf, buf_sz);
75251018Sgonzo	    if (sz != tests[i].size)
76251018Sgonzo		errx(1, "sz (%lu) != tests[%d].size (%lu) with in size %lu",
77251018Sgonzo		     (unsigned long)sz, i,
78251018Sgonzo		     (unsigned long)tests[i].size,
79251018Sgonzo		     (unsigned long)buf_sz);
80251018Sgonzo	    if (buf_sz > 0 && memcmp(buf, tests[i].str, buf_sz - 1) != 0)
81251018Sgonzo		errx(1, "test %i wrong result %s vs %s", i, buf, tests[i].str);
82251018Sgonzo	    if (buf_sz > 0 && buf[buf_sz - 1] != '\0')
83251018Sgonzo		errx(1, "test %i not zero terminated", i);
84251018Sgonzo	    rk_test_mem_free("overrun");
85251018Sgonzo
86251018Sgonzo	    buf = rk_test_mem_alloc(RK_TM_UNDERRUN, "underrun",
87251018Sgonzo				    NULL, tests[i].size);
88251018Sgonzo	    sz = unparse_time(tests[i].val, buf, min(buf_sz, tests[i].size));
89251018Sgonzo	    if (sz != tests[i].size)
90251018Sgonzo		errx(1, "sz (%lu) != tests[%d].size (%lu) with insize %lu",
91251018Sgonzo		     (unsigned long)sz, i,
92251018Sgonzo		     (unsigned long)tests[i].size,
93251018Sgonzo		     (unsigned long)buf_sz);
94251018Sgonzo	    if (buf_sz > 0 && strncmp(buf, tests[i].str, min(buf_sz, tests[i].size) - 1) != 0)
95251018Sgonzo		errx(1, "test %i wrong result %s vs %s", i, buf, tests[i].str);
96251018Sgonzo	    if (buf_sz > 0 && buf[min(buf_sz, tests[i].size) - 1] != '\0')
97251018Sgonzo		errx(1, "test %i not zero terminated", i);
98251018Sgonzo	    rk_test_mem_free("underrun");
99251018Sgonzo	}
100251018Sgonzo
101251018Sgonzo	buf = rk_test_mem_alloc(RK_TM_OVERRUN, "overrun",
102251018Sgonzo				tests[i].str, tests[i].size + 1);
103251018Sgonzo	j = parse_time(buf, "s");
104251018Sgonzo	if (j != tests[i].val)
105251018Sgonzo	    errx(1, "parse_time failed for test %d", i);
106251018Sgonzo	rk_test_mem_free("overrun");
107251018Sgonzo
108251018Sgonzo	buf = rk_test_mem_alloc(RK_TM_UNDERRUN, "underrun",
109251018Sgonzo				tests[i].str, tests[i].size + 1);
110251018Sgonzo	j = parse_time(buf, "s");
111251018Sgonzo	if (j != tests[i].val)
112251018Sgonzo	    errx(1, "parse_time failed for test %d", i);
113251018Sgonzo	rk_test_mem_free("underrun");
114251018Sgonzo
115251018Sgonzo    }
116251018Sgonzo    return 0;
117251018Sgonzo}
118251018Sgonzo