increase test coverage
[calc.git] / calc.c
1 /* depend: */
2 /* cflags: */
3 /* linker: atoi.o debug.o fdprintf.o parser.o -lm */
4
5 //#include <malloc.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <unistd.h>
9
10 #include "atoi.h"
11 #include "debug.h"
12 #include "fdprintf.h"
13 #include "parser.h"
14
15 /* constants */
16
17 //#define BUFFER_SIZE 4096
18 #define BUFFER_SIZE 256
19
20 /* macros */
21
22 #define CEIL(x, y) (((x) + (y) - 1) / (y))
23 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
24 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
25
26 /* gobal variables */
27
28 char *progname = NULL;
29 int precision = 6;
30
31 /* help function */
32
33 int usage (int ret)
34 {
35 int fd = ret ? stdfderr : stdfdout;
36 fdprintf (fd, "usage: %s\n", progname);
37 fdprintf (fd, " -h : help message\n");
38 fdprintf (fd, " -p : precision (%d)\n", precision);
39 fdprintf (fd, " -v : verbose level (%d)\n", verbose);
40
41 return ret;
42 }
43
44 /* main function */
45
46 int main (int argc, char *argv[])
47 {
48 char buffer[BUFFER_SIZE + 1] = {0};
49 char *pt = buffer;
50 int i = 0, j = 0, n;
51 int ret = 0;
52
53 /* program name */
54
55 progname = argv[0];
56 while (progname[i] != '\0') {
57 if ((progname[i] == '/') || (progname[i] == '\\')) {
58 progname += i + 1;
59 i = 0;
60 } else {
61 i++;
62 }
63 }
64
65 /* argument processing */
66
67 while (argc-- > 1) {
68 char *arg = *(++argv);
69 if (arg[0] != '-') {
70 PRINTERR ("%s: invalid option -- %s\n", progname, arg);
71 return usage (1);
72 }
73 char c = arg[1];
74 switch (c) {
75 case 'p':
76 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
77 if (arg == NULL) {
78 PRINTERR ("%s: missing precision\n", progname);
79 return usage (1);
80 }
81 precision = atoi (arg);
82 break;
83 case 'v':
84 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
85 if (arg == NULL) {
86 PRINTERR ("%s: missing verbose level\n", progname);
87 return usage (1);
88 }
89 verbose = atoi (arg);
90 break;
91 case 'h':
92 default:
93 return usage (c != 'h');
94 }
95 }
96
97 /* format */
98 char format[8] = "=> %.f\n";
99 format[4] = '0' + precision;
100
101 /* read from input stream */
102
103 while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
104 VERBOSE (INFO, PRINTOUT ("read %d bytes\n", n));
105 n += (pt - buffer);
106 if ((n == 2) && (buffer[0] == '.')) {
107 return 0;
108 }
109
110 /* look for end of line */
111 for (i = 0, j = 0; i < n; i++) {
112 if (buffer[i] == '\n') {
113 buffer[i] = 0;
114 VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j));
115 element_t *element = parser (buffer + j, NULL, 0);
116 if (element == ERROR_OP) {
117 VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
118 ret = 1;
119 } else {
120 VERBOSE (INFO, print_element (element, 0));
121 PRINTOUT (format, evaluate_element (element, 0));
122 delelement (element);
123 ret = 0;
124 }
125 //fsync (stdfdout);
126 j = i + 1;
127 }
128 }
129
130 /* keep remainding */
131 if (j < n) {
132 for (i = 0; i < n - j; i++) {
133 buffer[i] = buffer[i + j];
134 }
135 pt = buffer + n - j;
136 for (i = n - j; i < BUFFER_SIZE; i++) {
137 buffer[i] = 0;
138 }
139 }
140 }
141
142 return ret;
143 }
144
145 // test: calc.exe -h
146 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
147 // test: echo 1 | calc.exe -v3 | grep -q value
148 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
149 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
150 // test: calc.exe error 2>&1 | grep -q 'invalid option'
151 // test: calc.exe -p 2>&1 | grep -q 'missing precision'
152 // test: calc.exe -v 2>&1 | grep -q 'missing verbose'
153 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
154 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
155 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
156 // test: echo "1 / 2" | calc.exe | grep -q '=> 5e-1'
157 // test: echo "8 % 3" | calc.exe | grep -q '=> 2'
158 // test: echo "-9 % 3.1" | calc.exe | grep -q '=> -2.8'
159 // test: echo "2 ^ 3" | calc.exe | grep -q '=> 8'
160 // test: echo "1e-1 + 2.34e3" | calc.exe | grep -q '=> 2.3401'
161 // test: echo "sqrt (2)" | calc.exe | grep -q '=> 1.414213'
162 // test: echo "pow (2, 3)" | calc.exe | grep -q '=> 8'
163 // test: echo "cos (2)" | calc.exe | grep -q '=> -4.161468e-1'
164 // test: echo "sin (2)" | calc.exe | grep -q '=> 9.092974e-1'
165 // test: echo "atan (2)" | calc.exe | grep -q '=> 1.107148'
166 // test: echo "exp (2)" | calc.exe | grep -q '=> 7.389056'
167 // test: echo "log (2)" | calc.exe | grep -q '=> 6.931471e-1'
168 // test: echo "2 + 3 - 4" | calc.exe | grep -q '=> 1'
169 // test: echo "1 + cos (2 - 3)" | calc.exe | grep -q '=> 1.54030'
170 // test: echo "cos (1 / 2) * 3" | calc.exe | grep -q '=> 2.63274'
171 // test: echo "1 + 4 * (2 - 3)" | calc.exe | grep -q '=> -3'
172 // test: echo "(2 - 3) / 4" | calc.exe | grep -q '=> -2.5e-1'
173 // test: echo "pow (8 - 3, 4 / 3)" | calc.exe | grep -q '=> 8.549879'
174 // test: echo "1 + -2" | calc.exe | grep -q '=> -1'
175 // test: echo "1 - +2" | calc.exe | grep -q '=> -1'
176 // test: echo "-1 + +2" | calc.exe | grep -q '=> 1'
177 // test: echo "-1+2" | calc.exe | grep -q '=> 1'
178 // test: echo "1-2" | calc.exe | grep -q '=> -1'
179 // test: echo "1 * 2 / 3 + 4" | calc.exe | grep -q '=> 4.666666'
180 // test: echo "2 ^ 3 * 4 + 5" | calc.exe | grep -q '=> 3.7e1'
181 // test: echo "2 + 3 * 4 ^ 5" | calc.exe | grep -q '=> 3.074e3'
182 // test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe | grep -q '=> 3.267241e1'
183 // test: echo "95-6.3*15-1" | calc.exe | grep -q '=> -5e-1'
184 // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -5e-1'
185 // test: echo "95-6.3+15" | calc.exe | grep -q '=> 1.037e2'
186 // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0'
187 // test: echo "quit" | calc.exe | grep -q 'bye'
188 // test: echo "help" | calc.exe | grep -q 'miscellaneous'
189 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
190 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
191 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
192 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
193 // test: echo "pow (2)" | calc.exe | grep -q 'error'
194 // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
195 // test: echo . | calc.exe
196 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | ./calc.exe | grep -q 6.4e1
197 // test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\natan (0)\nlog (1)\nexp (1)\nhelp\nquit' | calc.exe -v 3 | grep -q bye
198 // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe |grep -c error |xargs test 11 =
199
200 /* vim: set ts=4 sw=4 et: */