OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / bitvector / examples / test.c
1
2 /*****************************************************************************/
3 /*                                                                           */
4 /*    Copyright (c) 2002 - 2004 by Steffen Beyer.                            */
5 /*    All rights reserved.                                                   */
6 /*                                                                           */
7 /*    This program is free software; you can redistribute it                 */
8 /*    and/or modify it under the same terms as Perl itself.                  */
9 /*                                                                           */
10 /*****************************************************************************/
11
12 /******************************************************/
13 /*                                                    */
14 /*  Example for using the BitVector.c library from C  */
15 /*                                                    */
16 /*  (Just for playing around; also shows how one can  */
17 /*   deal with error handling)                        */
18 /*                                                    */
19 /*  Copy ToolBox.h and BitVector.h to this directory  */
20 /*  then compile this file and link with BitVector.o  */
21 /*                                                    */
22 /******************************************************/
23
24 #include <stdio.h>
25 #include "ToolBox.h"
26 #include "BitVector.h"
27
28 void ListErrCode(ErrCode code)
29 {
30     if (code) fprintf(stdout, "BitVector ErrCode %2d: %s\n", code, BitVector_Error(code));
31 }
32
33 void PrintErrorMessage(ErrCode code, char *name)
34 {
35     if (code) fprintf(stdout, "Bit::Vector::%s(): %s\n", name, BitVector_Error(code));
36 }
37
38 #define FATAL_ERROR(name,code) \
39 { fprintf(stderr, "Bit::Vector::" name "(): %s\n", BitVector_Error(code)); exit(code); }
40
41 int main(void)
42 {
43     N_int bits = 5;
44 //  N_char x[] = "001010";  /* 10 */
45     N_char x[] = "001x10";  /* 10 */
46 //  N_char y[] = "000100";  /*  4 */
47 //  N_char y[] = "000111";  /*  7 */
48     N_char y[] = "001111";  /* 63 */
49     wordptr X;
50     wordptr Y;
51     wordptr Z;
52     ErrCode err;
53     boolean ovrflw;
54     boolean carry = false;
55
56     ListErrCode( 0);
57     ListErrCode( 1);
58     ListErrCode( 2);
59     ListErrCode( 3);
60     ListErrCode( 4);
61     ListErrCode( 5);
62     ListErrCode( 6);
63     ListErrCode( 7);
64     ListErrCode( 8);
65     ListErrCode( 9);
66     ListErrCode(10);
67     ListErrCode(11);
68     ListErrCode(12);
69     ListErrCode(13);
70     ListErrCode(14);
71     ListErrCode(15);
72     ListErrCode(16);
73     ListErrCode(17);
74     ListErrCode(18);
75     ListErrCode(19);
76     ListErrCode(20);
77
78     err = BitVector_Boot();
79     if (err) FATAL_ERROR("Boot", err);
80
81     printf("Number of bits in a WORD: %d\n", BitVector_Word_Bits());
82     printf("Number of bits in a LONG: %d\n", BitVector_Long_Bits());
83
84     X = BitVector_Create(bits, 1);
85     if (X == NULL) FATAL_ERROR("Create", ErrCode_Null);
86
87     err = BitVector_from_Bin(X, x);
88     PrintErrorMessage(err,"from_Bin");
89
90     Y = BitVector_Create(bits, 1);
91     if (Y == NULL) FATAL_ERROR("Create", ErrCode_Null);
92
93     err = BitVector_from_Bin(Y, y);
94     PrintErrorMessage(err,"from_Bin");
95
96     Z = BitVector_Create(bits, 1);
97     if (Z == NULL) FATAL_ERROR("Create", ErrCode_Null);
98
99     ovrflw = BitVector_add(Z, X, Y, &carry);
100
101     printf("result of %s + %s is %s (carry = %d, overflow = %d)\n",
102         BitVector_to_Dec(X),
103         BitVector_to_Dec(Y),  /* Beware the memory leaks here! */
104         BitVector_to_Dec(Z),  /* (Call "BitVector_Dispose()"!) */
105         carry, ovrflw);
106
107     err = BitVector_Multiply(Z, X, Y);
108
109     printf("result of %s * %s is %s\n",
110         BitVector_to_Dec(X),
111         BitVector_to_Dec(Y),  /* Beware the memory leaks here! */
112         BitVector_to_Dec(Z)); /* (Call "BitVector_Dispose()"!) */
113
114     PrintErrorMessage(err,"Multiply");
115
116     return(0);
117 }
118