Correctly populate the constant and function menus (Bug #527545)
[gcalctool.git] / gcalctool / unittest.c
blob7e9801386a86ec8ebf724744edd8a54c98416e4e
2 /* $Header$
4 * Copyright (c) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #include "unittest.h"
24 #include "display.h"
25 #include "functions.h"
26 #include "calctool.h"
28 static void
29 test(char *expression, char *expected, int expected_error)
31 int error;
32 int result[MP_SIZE];
33 char result_str[MAXLINE];
35 error = ce_parse(expression, result);
36 if(error != 0 || error != expected_error)
38 if(error == expected_error)
39 printf("SUCCESS: '%s' -> error %d\n", expression, error);
40 else
41 printf("FAIL: '%s' -> error %d, expected error %d and result '%s'\n", expression, error, expected_error, expected);
42 return;
45 make_fixed(result, result_str, DEC, 10, FALSE);
46 if(strcmp(result_str, expected) != 0)
47 printf("FAIL: '%s' -> '%s', expected '%s'\n", expression, result_str, expected);
48 else
49 printf("SUCCESS: '%s' -> '%s'\n", expression, result_str);
53 void
54 test_parser()
56 v->modetype = SCIENTIFIC;
58 test("0", "0", 0);
59 test("1", "1", 0);
60 test("+1", "1", 0);
61 test("++1", "1", 0);
62 test("--1", "1", 0);
63 test("255", "255", 0);
64 test("256", "256", 0);
65 test("1.00", "1", 0);
66 test("1.01", "1.01", 0);
67 test("1e9", "1000000000", 0);
69 test("0+0", "0", 0);
70 test("1+1", "2", 0);
71 test("2-3", "-1", 0);
72 test("2*3", "6", 0);
73 //FIXME: Need to update mperr() test("1/2", "0.5", 0);
74 //FIXME: Need to update mperr() test("1/0", "", 0);
75 //FIXME: Need to update mperr() test("0/0", "", 0);
77 test("1+2*3", "7", 0);
78 test("(1+2)*3", "9", 0);
80 test("100%", "1", 0);
81 test("1%", "0.01", 0);
82 test("2^2", "4", 0);
83 test("2^-1", "0.5", 0);
84 test("0!", "1", 0);
85 test("1!", "1", 0);
86 test("5!", "120", 0);
87 //FIXME: Need to update do_factorial() test("0.1!", "", 0);
88 //FIXME: Need to update do_factorial() test("-1!", "", 0);
90 test("-10^2", "-100", 0);
91 test("(-10)^2", "100", 0);
93 test("Sqrt(4)", "2", 0);
94 test("Sqrt(2)", "1.4142135", 0);
96 test("Int(3.2)", "3", 0);
97 test("Frac(3.2)", "0.2", 0);
98 test("Int(-3.2)", "-3", 0);
99 test("Frac(-3.2)", "-0.2", 0);
101 test("Abs(1)", "1", 0);
102 test("Abs(-1)", "1", 0);
104 test("Sin(0)", "0", 0);
105 test("Cos(0)", "1", 0);
106 test("Tan(0)", "0", 0);
108 v->ttype = DEG;
109 test("Sin(90)", "1", 0);
111 v->ttype = RAD;
112 test("Sin(3.14159/2)", "1", 0);
114 v->ttype = GRAD;
115 test("Sin(100)", "1", 0);
119 void
120 unittest()
122 test_parser();
124 exit(1);