rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

test_real2.h (4857B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "rsys.h"
     17 #include <float.h>
     18 
     19 #define REALX_DIMENSION__ 2
     20 #include "realX_begin.h"
     21 #define REAL REAL_TYPE__
     22 #define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__
     23 
     24 #define CHECK_REAL2(a, b)                                                      \
     25   {                                                                            \
     26     REAL* a__ = (a);                                                           \
     27     REAL* b__ = (b);                                                           \
     28     CHK((a__)[0] ==  (b__)[0]);                                                 \
     29     CHK((a__)[1] ==  (b__)[1]);                                                 \
     30   } (void) 0
     31 
     32 int
     33 main(int argc, char** argv)
     34 {
     35   REAL a[2], b[2], dst[2], f, c[2];
     36   REAL_COMPATIBLE d[2];
     37   (void)argc, (void)argv;
     38 
     39   REALX_FUNC__(set)(a, REALX_FUNC__(splat)(c, -1.0));
     40   CHECK_REAL2(a, c);
     41   CHK(a[0] ==  -1.0);
     42   CHK(a[1] ==  -1.0);
     43   REALX_FUNC__(set)(a, REALX_CTOR__(c, 0.0, 1.0));
     44   CHK(a[0] ==  0.0);
     45   CHK(a[1] ==  1.0);
     46   REALX_FUNC__(splat)(a, -2.0);
     47   CHK(a[0] ==  -2.0);
     48   CHK(a[1] ==  -2.0);
     49 
     50   REALX_FUNC__(set)(a, REALX_CTOR__(c, -1.0, 2.0));
     51   CHECK_REAL2(REALX_FUNC__(minus)(b, a), REALX_CTOR__(c, 1.0, -2.0));
     52   CHECK_REAL2(b, REALX_CTOR__(c, 1.0, -2.0));
     53 
     54   d[0] = (REAL_COMPATIBLE)0.1;
     55   d[1] = (REAL_COMPATIBLE)(1.0/3.0);
     56   REALX_CTOR__(c, (REAL)(REAL_COMPATIBLE)0.1, (REAL)(REAL_COMPATIBLE)(1.0/3.0));
     57   CHECK_REAL2(REALX_CAST__(dst, d), c);
     58 
     59   CHECK_REAL2(REALX_REAL_FUNC__(add)(dst, a, 1.0), REALX_CTOR__(c, 0.0, 3.0));
     60   CHECK_REAL2(dst, REALX_CTOR__(c, 0.0, 3.0));
     61   CHECK_REAL2(REALX_FUNC__(add)(dst, a, b), REALX_FUNC__(splat)(c, 0.0));
     62   CHECK_REAL2(dst, REALX_FUNC__(splat)(c, 0.0));
     63   CHECK_REAL2(REALX_REAL_FUNC__(sub)(dst, a, 1.0), REALX_CTOR__(c, -2.0, 1.0));
     64   CHECK_REAL2(dst, REALX_CTOR__(c, -2.0, 1.0));
     65   CHECK_REAL2(REALX_FUNC__(sub)(dst, a, b), REALX_CTOR__(c, -2.0, 4.0));
     66   CHECK_REAL2(dst, REALX_CTOR__(c, -2.0, 4.0));
     67   CHECK_REAL2(REALX_REAL_FUNC__(mul)(dst, a, 2.0), REALX_CTOR__(c, -2.0, 4.0));
     68   CHECK_REAL2(dst, REALX_CTOR__(c, -2.0, 4.0));
     69   CHECK_REAL2(REALX_FUNC__(mul)(dst, a, b), REALX_CTOR__(c, -1.0, -4.0));
     70   CHECK_REAL2(dst, REALX_CTOR__(c, -1.0, -4.0));
     71   CHECK_REAL2(REALX_FUNC__(div)(dst, dst, a), REALX_CTOR__(c, 1.0, -2.0));
     72   CHECK_REAL2(dst, REALX_CTOR__(c, 1.0, -2.0));
     73   CHECK_REAL2(REALX_REAL_FUNC__(div)(dst, a, 2.0), REALX_CTOR__(c, -0.5, 1.0));
     74   CHECK_REAL2(dst, REALX_CTOR__(c, -0.5, 1.0));
     75 
     76   REALX_FUNC__(set)(a, REALX_CTOR__(c, 0.0, 1.0));
     77   REALX_FUNC__(set)(b, REALX_CTOR__(c, 1.0, 2.0));
     78   CHECK_REAL2(REALX_FUNC__(lerp)(dst, a, b, 0.5), REALX_CTOR__(c, 0.5, 1.5));
     79   CHECK_REAL2(dst, REALX_CTOR__(c, 0.5, 1.5));
     80   CHK(REALX_FUNC__(sum)(b) ==  3.0);
     81   CHK(REALX_FUNC__(dot)(a, b) ==  2.0);
     82   CHK(eq_eps(REALX_FUNC__(len)(a), sqrt(1.0), REAL_EPSILON__) ==  1);
     83 
     84   CHK(REALX_FUNC__(is_normalized)(b) ==  0);
     85   f = REALX_FUNC__(normalize)(dst, b);
     86   CHK(REALX_FUNC__(is_normalized)(b) ==  0);
     87   CHK(REALX_FUNC__(is_normalized)(dst) != 0);
     88   CHK(eq_eps(f, sqrt(5.0), REAL_EPSILON__) ==  1);
     89   CHK(eq_eps(dst[0], b[0] / f, REAL_EPSILON__) ==  1);
     90   CHK(eq_eps(dst[1], b[1] / f, REAL_EPSILON__) ==  1);
     91 
     92   CHK(REALX_FUNC__(eq)(a, a) ==  1);
     93   CHK(REALX_FUNC__(eq)(a, b) ==  0);
     94   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, b[0], a[1])) ==  0);
     95   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, a[0], b[1])) ==  0);
     96 
     97   REALX_FUNC__(set)(b, a);
     98   REALX_FUNC__(add)(b, b, REALX_FUNC__(splat)(c, (REAL)(REAL_EPSILON__ * 0.5)));
     99   CHK(REALX_FUNC__(eq)(a, b) ==  0);
    100   CHK(REALX_FUNC__(eq_eps)(a, b, REAL_EPSILON__) ==  1);
    101   CHK(REALX_FUNC__(eq_eps)(a, b, (REAL)(REAL_EPSILON__ * 0.25)) ==  0);
    102 
    103   REALX_FUNC__(set)(a, REALX_CTOR__(c, 1.0, -2.0));
    104   REALX_FUNC__(set)(b, REALX_CTOR__(c, 3.0, 1.0));
    105   CHK(REALX_FUNC__(cross)(a, b) ==  7.0);
    106 
    107   REALX_FUNC__(set)(a, REALX_CTOR__(c, 1.0, -2.0));
    108   REALX_FUNC__(set)(b, REALX_CTOR__(c, -3.0, 1.0));
    109   CHECK_REAL2(REALX_FUNC__(max)(dst, a, b), REALX_CTOR__(c, 1.0, 1.0));
    110   CHECK_REAL2(dst, REALX_CTOR__(c, 1.0, 1.0));
    111   CHECK_REAL2(REALX_FUNC__(min)(dst, a, b), REALX_CTOR__(c, -3.0, -2.0));
    112   CHECK_REAL2(dst, REALX_CTOR__(c, -3.0, -2.0));
    113   return 0;
    114 }