Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2022 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : /*
21 : * Purpose: test smp library.
22 : */
23 : #undef NDEBUG
24 : #include <config.h>
25 :
26 : #include <assert.h>
27 : #include <stdio.h>
28 : #include <stdlib.h>
29 : #include <string.h>
30 :
31 : #include <freetds/bool.h>
32 : #include <freetds/utils/smp.h>
33 :
34 : static void
35 80 : same_smp(smp n, const char *s, int line)
36 : {
37 80 : char *out = smp_to_string(n);
38 80 : if (strcmp(s, out) != 0) {
39 0 : fprintf(stderr, "%d: Wrong number, expected %s got %s\n", line, s, out);
40 0 : free(out);
41 0 : exit(1);
42 : }
43 80 : free(out);
44 80 : }
45 : #define same_smp(n, s) same_smp(n, s, __LINE__)
46 :
47 : static void
48 40 : same_int(int n, int expected, int line)
49 : {
50 40 : if (n != expected) {
51 0 : fprintf(stderr, "%d: Wrong number, expected %d got %d\n", line, expected, n);
52 0 : exit(1);
53 : }
54 40 : }
55 : #define same_int(n, e) same_int(n, e, __LINE__)
56 :
57 :
58 8 : int main(void)
59 : {
60 : smp n;
61 : smp a;
62 :
63 8 : n = smp_from_int(1234567890123l);
64 8 : same_smp(n, "1234567890123");
65 8 : printf("%.20g\n", smp_to_double(n));
66 :
67 8 : n = smp_add(n, n);
68 8 : same_smp(n, "2469135780246");
69 8 : printf("%.20g\n", smp_to_double(n));
70 :
71 8 : printf("is_negative %d\n", smp_is_negative(n));
72 8 : same_int(smp_is_negative(n), false);
73 :
74 8 : n = smp_negate(n);
75 8 : same_smp(n, "-2469135780246");
76 8 : printf("%.20g\n", smp_to_double(n));
77 :
78 8 : printf("is_negative %d\n", smp_is_negative(n));
79 8 : same_int(smp_is_negative(n), true);
80 :
81 8 : n = smp_from_int(-87654321);
82 8 : same_smp(n, "-87654321");
83 8 : printf("%.20g\n", smp_to_double(n));
84 :
85 8 : a = smp_from_int(87654321);
86 8 : same_smp(a, "87654321");
87 :
88 8 : n = smp_add(n, a);
89 8 : same_smp(n, "0");
90 8 : printf("%.20g\n", smp_to_double(n));
91 :
92 8 : n = smp_sub(n, a);
93 8 : same_smp(n, "-87654321");
94 8 : printf("%.20g\n", smp_to_double(n));
95 :
96 8 : n = smp_from_int(4611686018427387904l);
97 8 : same_smp(n, "4611686018427387904");
98 :
99 8 : n = smp_add(n, n);
100 8 : same_smp(n, "9223372036854775808");
101 :
102 8 : n = smp_add(n, n);
103 8 : same_smp(n, "18446744073709551616");
104 :
105 8 : same_int(smp_cmp(smp_from_int(123), smp_from_int(123)), 0);
106 8 : same_int(smp_cmp(smp_from_int(123), smp_from_int(-124)), 1);
107 8 : same_int(smp_cmp(smp_from_int(-123), smp_from_int(123)), -1);
108 : return 0;
109 : }
110 :
|