File: test_record_array.c

package info (click to toggle)
haskell-futhark 0.25.32-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,236 kB
  • sloc: haskell: 100,484; ansic: 12,100; python: 3,440; yacc: 785; sh: 561; javascript: 558; lisp: 399; makefile: 277
file content (123 lines) | stat: -rw-r--r-- 4,273 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
#include "record_array.h"
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

void test1(struct futhark_context *ctx) {
  char* err;
  int32_t a[] = {1,2,3};
  float b[] = {1,2,3};

  struct futhark_i32_1d *a_fut = futhark_new_i32_1d(ctx, a, 3);
  assert(a_fut != NULL);
  struct futhark_f32_1d *b_fut = futhark_new_f32_1d(ctx, b, 3);
  assert(b_fut != NULL);
  struct futhark_f32_1d *b_short_fut = futhark_new_f32_1d(ctx, b, 2);
  assert(b_short_fut != NULL);

  struct futhark_opaque_arr1d_tup2_f32_f32 *b_b_fut;

  // Error case for zip.
  assert(futhark_zip_opaque_arr1d_tup2_f32_f32(ctx, &b_b_fut, b_fut, b_short_fut) == 1);
  err = futhark_context_get_error(ctx);
  assert(err != NULL);
  free(err);

  // Correct use of zip.
  assert(futhark_zip_opaque_arr1d_tup2_f32_f32(ctx, &b_b_fut, b_fut, b_fut) == 0);
  struct futhark_opaque_arr1d_tup2_i32_tup2_f32_f32 *a_b_b_fut;
  assert(futhark_zip_opaque_arr1d_tup2_i32_tup2_f32_f32(ctx, &a_b_b_fut, a_fut, b_b_fut) == 0);

  assert(futhark_shape_opaque_arr1d_tup2_i32_tup2_f32_f32(ctx, a_b_b_fut)[0] == 3);

  // Test indexing: out of bounds.
  assert(futhark_index_opaque_arr1d_tup2_i32_tup2_f32_f32(ctx, NULL, a_b_b_fut, 3) != 0);
  err = futhark_context_get_error(ctx);
  assert(err != NULL);
  free(err);

  // Test indexing: in bounds.
  struct futhark_opaque_tup2_i32_tup2_f32_f32* trip_fut;
  assert(futhark_index_opaque_arr1d_tup2_i32_tup2_f32_f32(ctx, &trip_fut, a_b_b_fut, 1) == 0);
  assert(futhark_context_sync(ctx) == 0); // XXX, would be nice if this was not required.
  struct futhark_opaque_tup2_f32_f32* pair_fut;
  assert(futhark_project_opaque_tup2_i32_tup2_f32_f32_1(ctx, &pair_fut, trip_fut) == 0);
  {
    int x;
    assert(futhark_project_opaque_tup2_i32_tup2_f32_f32_0(ctx, &x, trip_fut) == 0);
    assert(futhark_context_sync(ctx) == 0);
    assert(x == a[1]);
  }
  {
    float x;
    assert(futhark_project_opaque_tup2_f32_f32_0(ctx, &x, pair_fut) == 0);
    assert(futhark_context_sync(ctx) == 0);
    assert(x == b[1]);
  }
  {
    float x;
    assert(futhark_project_opaque_tup2_f32_f32_1(ctx, &x, pair_fut) == 0);
    assert(futhark_context_sync(ctx) == 0);
    assert(x == b[1]);
  }

  assert(futhark_free_opaque_tup2_f32_f32(ctx, pair_fut) == 0);
  assert(futhark_free_opaque_tup2_i32_tup2_f32_f32(ctx, trip_fut) == 0);
  assert(futhark_free_opaque_arr1d_tup2_i32_tup2_f32_f32(ctx, a_b_b_fut) == 0);
  assert(futhark_free_f32_1d(ctx, b_fut) == 0);
  assert(futhark_free_f32_1d(ctx, b_short_fut) == 0);
  assert(futhark_free_i32_1d(ctx, a_fut) == 0);
  assert(futhark_free_opaque_arr1d_tup2_f32_f32(ctx, b_b_fut) == 0);

}

void test2(struct futhark_context *ctx) {
  struct futhark_opaque_arr1d_tup2_arr1d_i32_f32 *a_b_fut;
  int32_t a[] = {1,2,3,4};
  float b[] = {5,6};

  struct futhark_i32_2d *a_fut = futhark_new_i32_2d(ctx, a, 2, 2);
  assert(a_fut != NULL);
  struct futhark_f32_1d *b_fut = futhark_new_f32_1d(ctx, b, 2);
  assert(b_fut != NULL);

  assert(futhark_zip_opaque_arr1d_tup2_arr1d_i32_f32(ctx, &a_b_fut, a_fut, b_fut) == 0);

  assert(futhark_free_f32_1d(ctx, b_fut) == 0);
  assert(futhark_free_i32_2d(ctx, a_fut) == 0);

  // Valid indexing.
  struct futhark_opaque_tup2_arr1d_i32_f32* a_b_elem_fut;
  assert(futhark_index_opaque_arr1d_tup2_arr1d_i32_f32(ctx, &a_b_elem_fut, a_b_fut, 1) == 0);

  {
    struct futhark_i32_1d *out_fut;
    assert(futhark_project_opaque_tup2_arr1d_i32_f32_0(ctx, &out_fut, a_b_elem_fut) == 0);
    int32_t out[2];
    assert(futhark_values_i32_1d(ctx, out_fut, out) == 0);
    assert(futhark_context_sync(ctx) == 0);
    assert(memcmp(out, &a[2], sizeof(int32_t)*2) == 0);
    assert(futhark_free_i32_1d(ctx, out_fut) == 0);
  }
  {
    float out;
    assert(futhark_project_opaque_tup2_arr1d_i32_f32_1(ctx, &out, a_b_elem_fut) == 0);
    assert(out == b[1]);
  }

  assert(futhark_free_opaque_arr1d_tup2_arr1d_i32_f32(ctx, a_b_fut) == 0);
  assert(futhark_free_opaque_tup2_arr1d_i32_f32(ctx, a_b_elem_fut) == 0);
}

int main() {
  struct futhark_context_config *cfg = futhark_context_config_new();
  struct futhark_context *ctx = futhark_context_new(cfg);
  assert(futhark_context_get_error(ctx) == NULL);

  test1(ctx);
  test2(ctx);

  futhark_context_free(ctx);
  futhark_context_config_free(cfg);
}