18c35491afd495d828e8f43e3293ef9d9e145c751hbono@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *  Copyright (C) 2001-2007  Peter Johnson
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE.
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdio.h>
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdlib.h>
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <string.h>
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "libyasm/bitvect.h"
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtest_boot(void)
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (BitVector_Boot() != ErrCode_Ok)
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return 1;
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return 0;
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct Val_s {
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    const char *ascii;
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned char result[10];   /* 80 bit result, little endian */
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} Val;
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgVal oct_small_vals[] = {
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {   "0",
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    },
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {   "1",
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    },
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {   "77",
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        {0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    },
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgVal oct_large_vals[] = {
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {   "7654321076543210",
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        {0x88, 0xC6, 0xFA, 0x88, 0xC6, 0xFA, 0x00, 0x00, 0x00, 0x00}
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    },
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {   "12634727612534126530214",
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        {0x8C, 0xB0, 0x5A, 0xE1, 0xAA, 0xF8, 0x3A, 0x67, 0x05, 0x00}
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    },
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    {   "61076543210",
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        {0x88, 0xC6, 0xFA, 0x88, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    },
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org};
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgwordptr testval;
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgnum_family_setup(void)
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    BitVector_Boot();
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    testval = BitVector_Create(80, FALSE);
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgnum_family_teardown(void)
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    BitVector_Destroy(testval);
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic char result_msg[1024];
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgnum_check(Val *val)
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned char ascii[64], *result;
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int len;
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int i;
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int ret = 0;
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    strcpy((char *)ascii, val->ascii);
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    strcpy(result_msg, "parser failure");
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if(BitVector_from_Oct(testval, ascii) != ErrCode_Ok)
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return 1;
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    result = BitVector_Block_Read(testval, &len);
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    for (i=0; i<10; i++)
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (result[i] != val->result[i])
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            ret = 1;
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (ret) {
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        strcpy(result_msg, val->ascii);
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        for (i=0; i<10; i++)
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            sprintf((char *)ascii+3*i, "%02x ", result[i]);
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        strcat(result_msg, ": ");
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        strcat(result_msg, (char *)ascii);
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    free(result);
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return ret;
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtest_oct_small_num(void)
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Val *vals = oct_small_vals;
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int i, num = sizeof(oct_small_vals)/sizeof(Val);
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    for (i=0; i<num; i++) {
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (num_check(&vals[i]) != 0)
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            return 1;
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return 0;
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtest_oct_large_num(void)
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Val *vals = oct_large_vals;
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int i, num = sizeof(oct_large_vals)/sizeof(Val);
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    for (i=0; i<num; i++) {
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (num_check(&vals[i]) != 0)
13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            return 1;
13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return 0;
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgchar failed[1000];
14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgruntest_(const char *testname, int (*testfunc)(void), void (*setup)(void),
14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org         void (*teardown)(void))
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int nf;
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (setup)
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        setup();
15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    nf = testfunc();
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (teardown)
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        teardown();
15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    printf("%c", nf>0 ? 'F':'.');
15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fflush(stdout);
15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (nf > 0)
15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        sprintf(failed, "%s ** F: %s failed!\n", failed, testname);
15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return nf;
16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define runtest(x,y,z)  runtest_(#x,test_##x,y,z)
16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint
16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgmain(void)
16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int nf = 0;
16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    failed[0] = '\0';
16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    printf("Test bitvect_test: ");
17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    nf += runtest(boot, NULL, NULL);
17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    nf += runtest(oct_small_num, num_family_setup, num_family_teardown);
17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    nf += runtest(oct_large_num, num_family_setup, num_family_teardown);
17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    printf(" +%d-%d/3 %d%%\n%s",
17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org           3-nf, nf, 100*(3-nf)/3, failed);
17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
177