~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/test/pgtypeslib/num_test.pgc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <pgtypes_numeric.h>
 
4
#include <decimal.h>
 
5
 
 
6
exec sql include ../regression;
 
7
 
 
8
 
 
9
/*
 
10
 
 
11
NOTE: This file has a different expect file for regression tests on MinGW32
 
12
 
 
13
*/
 
14
 
 
15
 
 
16
int
 
17
main(void)
 
18
{
 
19
        char *text="error\n";
 
20
        numeric *value1, *value2, *res;
 
21
        exec sql begin declare section;
 
22
                numeric(14,7) *des;
 
23
                /* = {0, 0, 0, 0, 0, NULL, NULL} ; */
 
24
        exec sql end declare section;
 
25
        double d;
 
26
        long l1, l2;
 
27
        int i;
 
28
 
 
29
        ECPGdebug(1, stderr);
 
30
        exec sql whenever sqlerror do sqlprint();
 
31
 
 
32
        exec sql connect to REGRESSDB1;
 
33
 
 
34
        exec sql set autocommit = off;
 
35
        exec sql create table test (text char(5), num numeric(14,7));
 
36
 
 
37
        value1 = PGTYPESnumeric_new();
 
38
        PGTYPESnumeric_from_int(1407, value1);
 
39
        text = PGTYPESnumeric_to_asc(value1, -1);
 
40
        printf("from int = %s\n", text);
 
41
        free(text);
 
42
        PGTYPESnumeric_free(value1);
 
43
 
 
44
        value1 = PGTYPESnumeric_from_asc("2369.7", NULL);
 
45
        value2 = PGTYPESnumeric_from_asc("10.0", NULL);
 
46
        res = PGTYPESnumeric_new();
 
47
        PGTYPESnumeric_add(value1, value2, res);
 
48
        text = PGTYPESnumeric_to_asc(res, -1);
 
49
        printf("add = %s\n", text);
 
50
        free(text);
 
51
 
 
52
        PGTYPESnumeric_sub(res, value2, res);
 
53
        text = PGTYPESnumeric_to_asc(res, -1);
 
54
        printf("sub = %s\n", text);
 
55
        free(text);
 
56
        PGTYPESnumeric_free(value2);
 
57
 
 
58
        des = PGTYPESnumeric_new();
 
59
        PGTYPESnumeric_copy(res, des);
 
60
        exec sql insert into test (text, num) values ('test', :des);
 
61
 
 
62
        value2 = PGTYPESnumeric_from_asc("2369.7", NULL);
 
63
        PGTYPESnumeric_mul(value1, value2, res);
 
64
        PGTYPESnumeric_free(value2);
 
65
 
 
66
        exec sql select num into :des from test where text = 'test';
 
67
 
 
68
        PGTYPESnumeric_mul(res, des, res);
 
69
        text = PGTYPESnumeric_to_asc(res, -1);
 
70
        printf("mul = %s\n", text);
 
71
        free(text);
 
72
        PGTYPESnumeric_free(des);
 
73
 
 
74
        value2 = PGTYPESnumeric_from_asc("10000", NULL);
 
75
        PGTYPESnumeric_div(res, value2, res);
 
76
        text = PGTYPESnumeric_to_asc(res, -1);
 
77
        PGTYPESnumeric_to_double(res, &d);
 
78
        printf("div = %s %e\n", text, d);
 
79
 
 
80
        value1 = PGTYPESnumeric_from_asc("2E7", NULL);
 
81
        value2 = PGTYPESnumeric_from_asc("14", NULL);
 
82
        i = PGTYPESnumeric_to_long(value1, &l1) | PGTYPESnumeric_to_long(value2, &l2);
 
83
        printf("to long(%d) = %ld %ld\n", i, l1, l2);
 
84
 
 
85
        free(text);
 
86
        PGTYPESnumeric_free(value1);
 
87
        PGTYPESnumeric_free(value2);
 
88
        PGTYPESnumeric_free(res);
 
89
 
 
90
        exec sql rollback;
 
91
        exec sql disconnect;
 
92
 
 
93
        return (0);
 
94
}
 
95