LCOV - code coverage report
Current view: top level - src/utils/unittests - smp.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 42 47 89.4 %
Date: 2025-02-21 09:36:06 Functions: 3 3 100.0 %

          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         100 : same_smp(smp n, const char *s, int line)
      36             : {
      37         100 :         char *out = smp_to_string(n);
      38         100 :         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         100 :         free(out);
      44         100 : }
      45             : #define same_smp(n, s) same_smp(n, s, __LINE__)
      46             : 
      47             : static void
      48          50 : same_int(int n, int expected, int line)
      49             : {
      50          50 :         if (n != expected) {
      51           0 :                 fprintf(stderr, "%d: Wrong number, expected %d got %d\n", line, expected, n);
      52           0 :                 exit(1);
      53             :         }
      54          50 : }
      55             : #define same_int(n, e) same_int(n, e, __LINE__)
      56             : 
      57             : 
      58          10 : int main(void)
      59             : {
      60             :         smp n;
      61             :         smp a;
      62             : 
      63          10 :         n = smp_from_int(1234567890123l);
      64          10 :         same_smp(n, "1234567890123");
      65          10 :         printf("%.20g\n", smp_to_double(n));
      66             : 
      67          10 :         n = smp_add(n, n);
      68          10 :         same_smp(n, "2469135780246");
      69          10 :         printf("%.20g\n", smp_to_double(n));
      70             : 
      71          10 :         printf("is_negative %d\n", smp_is_negative(n));
      72          10 :         same_int(smp_is_negative(n), false);
      73             : 
      74          10 :         n = smp_negate(n);
      75          10 :         same_smp(n, "-2469135780246");
      76          10 :         printf("%.20g\n", smp_to_double(n));
      77             : 
      78          10 :         printf("is_negative %d\n", smp_is_negative(n));
      79          10 :         same_int(smp_is_negative(n), true);
      80             : 
      81          10 :         n = smp_from_int(-87654321);
      82          10 :         same_smp(n, "-87654321");
      83          10 :         printf("%.20g\n", smp_to_double(n));
      84             : 
      85          10 :         a = smp_from_int(87654321);
      86          10 :         same_smp(a, "87654321");
      87             : 
      88          10 :         n = smp_add(n, a);
      89          10 :         same_smp(n, "0");
      90          10 :         printf("%.20g\n", smp_to_double(n));
      91             : 
      92          10 :         n = smp_sub(n, a);
      93          10 :         same_smp(n, "-87654321");
      94          10 :         printf("%.20g\n", smp_to_double(n));
      95             : 
      96          10 :         n = smp_from_int(4611686018427387904l);
      97          10 :         same_smp(n, "4611686018427387904");
      98             : 
      99          10 :         n = smp_add(n, n);
     100          10 :         same_smp(n, "9223372036854775808");
     101             : 
     102          10 :         n = smp_add(n, n);
     103          10 :         same_smp(n, "18446744073709551616");
     104             : 
     105          10 :         same_int(smp_cmp(smp_from_int(123), smp_from_int(123)), 0);
     106          10 :         same_int(smp_cmp(smp_from_int(123), smp_from_int(-124)), 1);
     107          10 :         same_int(smp_cmp(smp_from_int(-123), smp_from_int(123)), -1);
     108             :         return 0;
     109             : }
     110             : 

Generated by: LCOV version 1.13