Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Brian Bruns
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 : #include "common.h"
20 : #include <freetds/convert.h>
21 :
22 : static TDSCONTEXT ctx;
23 :
24 : int
25 10 : main(void)
26 : {
27 : TDSLOGIN *login;
28 : TDSSOCKET *tds;
29 10 : int verbose = 0;
30 : int rc;
31 : int i;
32 :
33 : /* variables for conversions */
34 : TDSCOLUMN *curcol;
35 : TDSRESULTINFO *resinfo;
36 : unsigned char *src;
37 :
38 : CONV_RESULT cr;
39 : TDS_INT srctype, srclen;
40 :
41 10 : int src_id = 0;
42 : double src_val;
43 : double src_err;
44 10 : double tolerance = 0.000001;
45 :
46 : char sql[256];
47 10 : int num_sybreal = 5;
48 : float sybreal[5];
49 10 : int num_sybflt8 = 7;
50 : double sybflt8[7];
51 : int result_type;
52 :
53 10 : memset(&ctx, 0, sizeof(ctx));
54 :
55 10 : sybreal[0] = (float) 1.1;
56 10 : sybreal[1] = (float) 12345678;
57 10 : sybreal[2] = (float) 0.012345678;
58 10 : sybreal[3] = (float) 1.234567890e+20;
59 10 : sybreal[4] = (float) 1.234567890e-20;
60 :
61 10 : sybflt8[0] = 1.1;
62 10 : sybflt8[1] = 1234567890123456.0;
63 10 : sybflt8[2] = 0.01234567890123456;
64 10 : sybflt8[3] = 1.234567890123456e+20;
65 10 : sybflt8[4] = 1.234567890123456e-20;
66 10 : sybflt8[5] = 1.234567890123456e+200;
67 10 : sybflt8[6] = 1.234567890123456e-200;
68 :
69 10 : printf("%s: Test SYBREAL, SYBFLT8 values\n", __FILE__);
70 10 : rc = try_tds_login(&login, &tds, __FILE__, verbose);
71 10 : if (rc != TDS_SUCCESS) {
72 0 : fprintf(stderr, "try_tds_login() failed\n");
73 0 : return 1;
74 : }
75 :
76 :
77 : /*
78 : * SYBREAL tests
79 : */
80 : if (verbose)
81 : printf("Starting SYBREAL tests\n");
82 10 : rc = run_query(tds, "DROP TABLE #test_table");
83 10 : if (rc != TDS_SUCCESS) {
84 : return 1;
85 : }
86 10 : rc = run_query(tds, "CREATE TABLE #test_table (id int, val real)");
87 10 : if (rc != TDS_SUCCESS) {
88 : return 1;
89 : }
90 :
91 50 : for (i = 0; i < num_sybreal; i++) {
92 50 : sprintf(sql, "INSERT #test_table (id, val) VALUES (%d, %.8g)", i, sybreal[i]);
93 : if (verbose)
94 : printf("%s\n", sql);
95 50 : rc = run_query(tds, sql);
96 50 : if (rc != TDS_SUCCESS) {
97 : return 1;
98 : }
99 : }
100 :
101 10 : rc = tds_submit_query(tds, "SELECT * FROM #test_table");
102 :
103 70 : while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) {
104 50 : switch (result_type) {
105 50 : case TDS_ROW_RESULT:
106 50 : resinfo = tds->res_info;
107 150 : for (i = 0; i < resinfo->num_cols; i++) {
108 100 : curcol = resinfo->columns[i];
109 100 : src = curcol->column_data;
110 : if (verbose) {
111 : srctype = curcol->column_type;
112 : srclen = curcol->column_size;
113 : tds_convert(&ctx, srctype, src, srclen, SYBCHAR, &cr);
114 : printf("col %i is %s\n", i, cr.c);
115 : }
116 100 : if (i == 0) {
117 50 : src_id = *(int *) src;
118 : } else {
119 50 : src_val = *(float *) src;
120 50 : src_err = src_val - sybreal[src_id];
121 50 : if (src_err != 0.0) {
122 0 : src_err = src_err / src_val;
123 : }
124 50 : if (src_err < -tolerance || src_err > tolerance) {
125 0 : fprintf(stderr, "SYBREAL expected %.8g got %.8g\n",
126 : sybreal[src_id], src_val);
127 0 : fprintf(stderr, "Error was %.4g%%\n", 100 * src_err);
128 0 : return 1;
129 : }
130 : }
131 : }
132 : case TDS_COMPUTE_RESULT:
133 : break;
134 0 : default:
135 0 : fprintf(stderr, "tds_process_tokens() unexpected result\n");
136 0 : break;
137 : }
138 : }
139 10 : if (rc != TDS_NO_MORE_RESULTS) {
140 0 : fprintf(stderr, "tds_process_tokens() unexpected return\n");
141 : }
142 :
143 :
144 : /*
145 : * SYBFLT8 tests
146 : */
147 : if (verbose)
148 : printf("Starting SYBFLT8 tests\n");
149 10 : rc = run_query(tds, "DROP TABLE #test_table");
150 10 : if (rc != TDS_SUCCESS) {
151 : return 1;
152 : }
153 10 : rc = run_query(tds, "CREATE TABLE #test_table (id int, val float(48))");
154 10 : if (rc != TDS_SUCCESS) {
155 : return 1;
156 : }
157 :
158 70 : for (i = 0; i < num_sybflt8; i++) {
159 70 : sprintf(sql, "INSERT #test_table (id, val) VALUES (%d, %.15g)", i, sybflt8[i]);
160 : if (verbose)
161 : printf("%s\n", sql);
162 70 : rc = run_query(tds, sql);
163 70 : if (rc != TDS_SUCCESS) {
164 : return 1;
165 : }
166 : }
167 :
168 10 : rc = tds_submit_query(tds, "SELECT * FROM #test_table");
169 90 : while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) {
170 70 : switch (result_type) {
171 70 : case TDS_ROW_RESULT:
172 70 : resinfo = tds->res_info;
173 210 : for (i = 0; i < resinfo->num_cols; i++) {
174 140 : curcol = resinfo->columns[i];
175 140 : src = curcol->column_data;
176 : if (verbose) {
177 : srctype = curcol->column_type;
178 : srclen = curcol->column_size;
179 : tds_convert(&ctx, srctype, src, srclen, SYBCHAR, &cr);
180 : printf("col %i is %s\n", i, cr.c);
181 : }
182 140 : if (i == 0) {
183 70 : src_id = *(int *) src;
184 : } else {
185 70 : memcpy(&src_val, src, 8);
186 70 : src_err = src_val - sybflt8[src_id];
187 70 : if (src_err != 0.0) {
188 60 : src_err = src_err / src_val;
189 : }
190 70 : if (src_err < -tolerance || src_err > tolerance) {
191 0 : fprintf(stderr, "SYBFLT8 expected %.16g got %.16g\n",
192 : sybflt8[src_id], src_val);
193 0 : fprintf(stderr, "Error was %.4g%%\n", 100 * src_err);
194 0 : return 1;
195 : }
196 : }
197 : }
198 : case TDS_COMPUTE_RESULT:
199 : break;
200 0 : default:
201 0 : fprintf(stderr, "tds_process_tokens() returned unexpected result\n");
202 0 : break;
203 : }
204 : }
205 10 : if (rc != TDS_NO_MORE_RESULTS) {
206 0 : fprintf(stderr, "tds_process_tokens() unexpected return\n");
207 : }
208 :
209 10 : try_tds_logout(login, tds, verbose);
210 10 : return 0;
211 : }
|