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