File: test_cpp.cpp

package info (click to toggle)
lfortran 0.58.0-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 54,512 kB
  • sloc: cpp: 162,179; f90: 68,251; python: 17,476; ansic: 6,278; yacc: 2,334; sh: 1,317; fortran: 892; makefile: 37; javascript: 15
file content (132 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// ptrdiff_t, size_t
#include <cstddef>

// int32_t
#include <cstdint>

#include <iostream>

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <tests/doctest.h>


struct descriptor_dimension
{
  ptrdiff_t stride;
  ptrdiff_t lower_bound;
  ptrdiff_t upper_bound;
};

template <int Rank, typename Type>
struct descriptor {
  Type *base_addr;
  size_t offset;
  ptrdiff_t dtype;
  descriptor_dimension dim[Rank];
};

template <int Rank, typename Type>
descriptor<Rank, Type> c_desc(Type *array_ptr,
        const std::vector<std::pair<int,int>> &dims) {
    descriptor<Rank, Type> a;
    a.base_addr = array_ptr;
    a.offset = 0;
    for (int n=0; n < Rank; n++) {
        a.dim[n].lower_bound = dims[n].first;
        a.dim[n].upper_bound = dims[n].second;

        if (n == 0) {
            a.dim[n].stride = 1;
        } else {
            a.dim[n].stride =
                (a.dim[n-1].upper_bound - a.dim[n-1].lower_bound + 1)
                * a.dim[n-1].stride;
        }
        a.offset = a.offset - a.dim[n].lower_bound * a.dim[n].stride;
    }
    a.dtype = Rank;
    // 1 is INTEGER element type
    a.dtype |= (1 << 3);
    // sizeof(Type) is the size of INTEGER
    a.dtype |= (sizeof(Type) << 6);
    return a;
}

#include "mod2.h"

TEST_CASE("f1")
{
    int a = 1;
    int b = 2;
    CHECK(__mod1_MOD_f1(&a, &b) == 3);
}

TEST_CASE("f2")
{
    int n = 3;
    std::vector<int32_t> data(3);
    data[0] = 1;
    data[1] = 2;
    data[2] = 3;
    CHECK(__mod1_MOD_f2(&n, &data[0]) == 6);
}

TEST_CASE("f2b")
{
    std::vector<int32_t> data(3);
    data[0] = 1;
    data[1] = 2;
    data[2] = 3;

    descriptor<1, int32_t> a = c_desc<1, int32_t>(&data[0],
            {{1, 3}});
    CHECK(__mod1_MOD_f2b(&a) == 6);
}

TEST_CASE("f3")
{
    std::vector<int32_t> data(6);
    data[0] = 1;
    data[1] = 2;
    data[2] = 3;
    data[3] = 4;
    data[4] = 5;
    data[5] = 6;

    int n = 2;
    int m = 3;

    CHECK(__mod1_MOD_f3(&n, &m, &data[0]) == 21);
}

TEST_CASE("f3b")
{
    std::vector<int32_t> data = {1, 2, 3, 4, 5, 6};
    descriptor<2, int32_t> a = c_desc<2, int32_t>(&data[0],
            {{1, 2}, {1, 3}});
    CHECK(__mod1_MOD_f3b(&a) == 21);
}

TEST_CASE("f4")
{
    int a = 1;
    int b = 0;
    CHECK(__mod1_MOD_f4(&a, &b) == 0);
    CHECK(b == 1);
}

TEST_CASE("f5b")
{
    std::vector<int32_t> data(6);
    data[0] = 1;
    data[1] = 2;
    data[2] = 3;
    data[3] = 4;
    data[4] = 5;
    data[5] = 6;

    descriptor<2, int32_t> a = c_desc<2, int32_t>(&data[0],
            {{1, 2}, {1, 3}});

    CHECK(__mod1_MOD_f5b(&a) == 9);
}