1119418Sobrien/*
266703Sarchie * Copyright (c) 1999 Kungliga Tekniska H�gskolan
366703Sarchie * (Royal Institute of Technology, Stockholm, Sweden).
4119418Sobrien * All rights reserved.
566703Sarchie *
666703Sarchie * Redistribution and use in source and binary forms, with or without
7119418Sobrien * modification, are permitted provided that the following conditions
866703Sarchie * are met:
966703Sarchie *
1066703Sarchie * 1. Redistributions of source code must retain the above copyright
1166703Sarchie *    notice, this list of conditions and the following disclaimer.
1266703Sarchie *
1366703Sarchie * 2. Redistributions in binary form must reproduce the above copyright
1466703Sarchie *    notice, this list of conditions and the following disclaimer in the
1566703Sarchie *    documentation and/or other materials provided with the distribution.
1666703Sarchie *
1766703Sarchie * 3. Neither the name of the Institute nor the names of its contributors
1866703Sarchie *    may be used to endorse or promote products derived from this software
1966703Sarchie *    without specific prior written permission.
2066703Sarchie *
2166703Sarchie * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2266703Sarchie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2366703Sarchie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2466703Sarchie * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2566703Sarchie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2666703Sarchie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2766703Sarchie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2866703Sarchie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2966703Sarchie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3066703Sarchie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3166703Sarchie * SUCH DAMAGE.
3266703Sarchie */
3366703Sarchie
3466703Sarchie#ifdef HAVE_CONFIG_H
3566703Sarchie#include <config.h>
3666703SarchieRCSID("$Id: parse_bytes-test.c 10655 2001-09-04 09:56:00Z assar $");
3766703Sarchie#endif
3866703Sarchie
39119418Sobrien#include "roken.h"
40119418Sobrien#include "parse_bytes.h"
41119418Sobrien
4266703Sarchiestatic struct testcase {
4366703Sarchie    int canonicalp;
44119618Snjl    int val;
4566703Sarchie    const char *def_unit;
4666703Sarchie    const char *str;
4766703Sarchie} tests[] = {
4866703Sarchie    {0, 0, NULL, "0 bytes"},
4966703Sarchie    {1, 0, NULL, "0"},
50129879Sphk    {0, 1, NULL, "1"},
5166703Sarchie    {1, 1, NULL, "1 byte"},
5274914Sjhb    {0, 0, "kilobyte", "0"},
5369734Sarchie    {0, 1024, "kilobyte", "1"},
5466703Sarchie    {1, 1024, "kilobyte", "1 kilobyte"},
5566703Sarchie    {1, 1024 * 1024, NULL, "1 megabyte"},
5666703Sarchie    {0, 1025, NULL, "1 kilobyte 1"},
5766703Sarchie    {1, 1025, NULL, "1 kilobyte 1 byte"},
5866703Sarchie};
5966703Sarchie
6066703Sarchieint
61119280Simpmain(int argc, char **argv)
62119280Simp{
6366703Sarchie    int i;
6466703Sarchie    int ret = 0;
6566703Sarchie
6666703Sarchie    for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
6766703Sarchie	char buf[256];
6866703Sarchie	int val = parse_bytes (tests[i].str, tests[i].def_unit);
6966703Sarchie	int len;
70105394Snyan
71105394Snyan	if (val != tests[i].val) {
7266703Sarchie	    printf ("parse_bytes (%s, %s) = %d != %d\n",
7396200Sjhb		    tests[i].str,
74101742Smp		    tests[i].def_unit ? tests[i].def_unit : "none",
75119600Snjl		    val, tests[i].val);
76131070Sambrisko	    ++ret;
7766703Sarchie	}
7866703Sarchie	if (tests[i].canonicalp) {
7966703Sarchie	    len = unparse_bytes (tests[i].val, buf, sizeof(buf));
8066703Sarchie	    if (strcmp (tests[i].str, buf) != 0) {
8166703Sarchie		printf ("unparse_bytes (%d) = \"%s\" != \"%s\"\n",
8266703Sarchie			tests[i].val, buf, tests[i].str);
8366703Sarchie		++ret;
8466703Sarchie	    }
8566703Sarchie	}
8666703Sarchie    }
8766703Sarchie    if (ret) {
8866703Sarchie	printf ("%d errors\n", ret);
8966703Sarchie	return 1;
9066703Sarchie    } else
9166703Sarchie	return 0;
9266703Sarchie}
9366703Sarchie