| 1 |
/* |
| 2 |
* Copyright (c) 2004 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
|
| 36 |
#include "roken.h" |
| 37 |
#include "parse_time.h" |
| 38 |
#include "test-mem.h" |
| 39 |
#include "err.h" |
| 40 |
|
| 41 |
static struct testcase { |
| 42 |
size_t size; |
| 43 |
int val; |
| 44 |
char *str; |
| 45 |
} tests[] = { |
| 46 |
{ 8, 1, "1 second" }, |
| 47 |
{ 17, 61, "1 minute 1 second" }, |
| 48 |
{ 18, 62, "1 minute 2 seconds" }, |
| 49 |
{ 8, 60, "1 minute" }, |
| 50 |
{ 6, 3600, "1 hour" }, |
| 51 |
{ 15, 3601, "1 hour 1 second" }, |
| 52 |
{ 16, 3602, "1 hour 2 seconds" }, |
| 53 |
{ 9, 300, "5 minutes" }, |
| 54 |
}; |
| 55 |
|
| 56 |
int |
| 57 |
main(int argc, char **argv) |
| 58 |
{ |
| 59 |
size_t sz; |
| 60 |
size_t buf_sz; |
| 61 |
int i, j; |
| 62 |
|
| 63 |
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) { |
| 64 |
char *buf; |
| 65 |
|
| 66 |
sz = unparse_time(tests[i].val, NULL, 0); |
| 67 |
if (sz != tests[i].size) |
| 68 |
errx(1, "sz (%lu) != tests[%d].size (%lu)", |
| 69 |
(unsigned long)sz, i, (unsigned long)tests[i].size); |
| 70 |
|
| 71 |
for (buf_sz = 0; buf_sz < tests[i].size + 2; buf_sz++) { |
| 72 |
|
| 73 |
buf = rk_test_mem_alloc(RK_TM_OVERRUN, "overrun", |
| 74 |
NULL, buf_sz); |
| 75 |
sz = unparse_time(tests[i].val, buf, buf_sz); |
| 76 |
if (sz != tests[i].size) |
| 77 |
errx(1, "sz (%lu) != tests[%d].size (%lu) with in size %lu", |
| 78 |
(unsigned long)sz, i, |
| 79 |
(unsigned long)tests[i].size, |
| 80 |
(unsigned long)buf_sz); |
| 81 |
if (buf_sz > 0 && memcmp(buf, tests[i].str, buf_sz - 1) != 0) |
| 82 |
errx(1, "test %i wrong result %s vs %s", i, buf, tests[i].str); |
| 83 |
if (buf_sz > 0 && buf[buf_sz - 1] != '\0') |
| 84 |
errx(1, "test %i not zero terminated", i); |
| 85 |
rk_test_mem_free("overrun"); |
| 86 |
|
| 87 |
buf = rk_test_mem_alloc(RK_TM_UNDERRUN, "underrun", |
| 88 |
NULL, tests[i].size); |
| 89 |
sz = unparse_time(tests[i].val, buf, min(buf_sz, tests[i].size)); |
| 90 |
if (sz != tests[i].size) |
| 91 |
errx(1, "sz (%lu) != tests[%d].size (%lu) with insize %lu", |
| 92 |
(unsigned long)sz, i, |
| 93 |
(unsigned long)tests[i].size, |
| 94 |
(unsigned long)buf_sz); |
| 95 |
if (buf_sz > 0 && strncmp(buf, tests[i].str, min(buf_sz, tests[i].size) - 1) != 0) |
| 96 |
errx(1, "test %i wrong result %s vs %s", i, buf, tests[i].str); |
| 97 |
if (buf_sz > 0 && buf[min(buf_sz, tests[i].size) - 1] != '\0') |
| 98 |
errx(1, "test %i not zero terminated", i); |
| 99 |
rk_test_mem_free("underrun"); |
| 100 |
} |
| 101 |
|
| 102 |
buf = rk_test_mem_alloc(RK_TM_OVERRUN, "overrun", |
| 103 |
tests[i].str, tests[i].size + 1); |
| 104 |
j = parse_time(buf, "s"); |
| 105 |
if (j != tests[i].val) |
| 106 |
errx(1, "parse_time failed for test %d", i); |
| 107 |
rk_test_mem_free("overrun"); |
| 108 |
|
| 109 |
buf = rk_test_mem_alloc(RK_TM_UNDERRUN, "underrun", |
| 110 |
tests[i].str, tests[i].size + 1); |
| 111 |
j = parse_time(buf, "s"); |
| 112 |
if (j != tests[i].val) |
| 113 |
errx(1, "parse_time failed for test %d", i); |
| 114 |
rk_test_mem_free("underrun"); |
| 115 |
|
| 116 |
} |
| 117 |
return 0; |
| 118 |
} |