LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/pgtypeslib - num_test.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 69 69 100.0 %
Date: 2024-04-19 19:11:30 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : exec sql include ../printf_hack;
       9             : 
      10             : 
      11             : int
      12           4 : main(void)
      13             : {
      14           4 :     char *text="error\n";
      15             :     numeric *value1, *value2, *res;
      16             :     exec sql begin declare section;
      17             :         numeric(14,7) *des;
      18             :         /* = {0, 0, 0, 0, 0, NULL, NULL} ; */
      19             :     exec sql end declare section;
      20             :     double d;
      21             :     long l1, l2;
      22             :     int i, min, max;
      23             : 
      24           4 :     ECPGdebug(1, stderr);
      25             :     exec sql whenever sqlerror do sqlprint();
      26             : 
      27           4 :     exec sql connect to REGRESSDB1;
      28           4 : 
      29           4 :     exec sql set autocommit = off;
      30           4 :     exec sql create table test (text char(5), num numeric(14,7));
      31           4 : 
      32           4 :     value1 = PGTYPESnumeric_new();
      33           4 :     PGTYPESnumeric_from_int(1407, value1);
      34           4 :     text = PGTYPESnumeric_to_asc(value1, -1);
      35           4 :     printf("from int = %s\n", text);
      36           4 :     PGTYPESchar_free(text);
      37           4 :     PGTYPESnumeric_free(value1);
      38             : 
      39           4 :     value1 = PGTYPESnumeric_from_asc("2369.7", NULL);
      40           4 :     value2 = PGTYPESnumeric_from_asc("10.0", NULL);
      41           4 :     res = PGTYPESnumeric_new();
      42           4 :     PGTYPESnumeric_add(value1, value2, res);
      43           4 :     text = PGTYPESnumeric_to_asc(res, -1);
      44           4 :     printf("add = %s\n", text);
      45           4 :     PGTYPESchar_free(text);
      46             : 
      47           4 :     PGTYPESnumeric_sub(res, value2, res);
      48           4 :     text = PGTYPESnumeric_to_asc(res, -1);
      49           4 :     printf("sub = %s\n", text);
      50           4 :     PGTYPESchar_free(text);
      51           4 :     PGTYPESnumeric_free(value2);
      52             : 
      53           4 :     des = PGTYPESnumeric_new();
      54           4 :     PGTYPESnumeric_copy(res, des);
      55           4 :     exec sql insert into test (text, num) values ('test', :des);
      56           4 : 
      57           4 :     value2 = PGTYPESnumeric_from_asc("2369.7", NULL);
      58           4 :     PGTYPESnumeric_mul(value1, value2, res);
      59           4 :     PGTYPESnumeric_free(value2);
      60             : 
      61           4 :     exec sql select num into :des from test where text = 'test';
      62           4 : 
      63           4 :     PGTYPESnumeric_mul(res, des, res);
      64           4 :     text = PGTYPESnumeric_to_asc(res, -1);
      65           4 :     printf("mul = %s\n", text);
      66           4 :     PGTYPESchar_free(text);
      67           4 :     PGTYPESnumeric_free(des);
      68             : 
      69           4 :     value2 = PGTYPESnumeric_from_asc("10000", NULL);
      70           4 :     PGTYPESnumeric_div(res, value2, res);
      71           4 :     text = PGTYPESnumeric_to_asc(res, -1);
      72           4 :     PGTYPESnumeric_to_double(res, &d);
      73           4 :     printf("div = %s ", text);
      74           4 :     print_double(d);
      75           4 :     printf("\n");
      76             : 
      77           4 :     PGTYPESnumeric_free(value1);
      78           4 :     PGTYPESnumeric_free(value2);
      79             : 
      80           4 :     value1 = PGTYPESnumeric_from_asc("2E7", NULL);
      81           4 :     value2 = PGTYPESnumeric_from_asc("14", NULL);
      82           4 :     i = PGTYPESnumeric_to_long(value1, &l1) | PGTYPESnumeric_to_long(value2, &l2);
      83           4 :     printf("to long(%d) = %ld %ld\n", i, l1, l2);
      84             : 
      85           4 :     PGTYPESchar_free(text);
      86           4 :     PGTYPESnumeric_free(value1);
      87           4 :     PGTYPESnumeric_free(value2);
      88           4 :     PGTYPESnumeric_free(res);
      89             : 
      90             :     /* check conversion of numeric to int */
      91           4 :     value1 = PGTYPESnumeric_from_asc("-2147483648", NULL);
      92           4 :     PGTYPESnumeric_to_int(value1, &min);
      93           4 :     printf("min int = %d\n", min);
      94           4 :     PGTYPESnumeric_free(value1);
      95             : 
      96           4 :     value2 = PGTYPESnumeric_from_asc("2147483647", NULL);
      97           4 :     PGTYPESnumeric_to_int(value2, &max);
      98           4 :     printf("max int = %d\n", max);
      99           4 :     PGTYPESnumeric_free(value2);
     100             : 
     101           4 :     exec sql rollback;
     102           4 :     exec sql disconnect;
     103           4 : 
     104           4 :     return 0;
     105             : }

Generated by: LCOV version 1.14