File: test_restore.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 (65 lines) | stat: -rw-r--r-- 1,592 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
#include "restore.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>

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

  int err;
  struct futhark_opaque_whatever *whatever;

  err = futhark_entry_mk(ctx, &whatever, 1);
  assert(err == 0);
  err = futhark_context_sync(ctx);
  assert(err == 0);

  void *bytes0 = NULL;
  void *bytes1;
  size_t n0, n1;

  err = futhark_store_opaque_whatever(ctx, whatever, &bytes0, &n0);
  assert(err == 0);
  err = futhark_store_opaque_whatever(ctx, whatever, NULL, &n1);
  assert(err == 0);
  assert(n0 == n1);
  bytes1 = malloc(n1);
  err = futhark_store_opaque_whatever(ctx, whatever, &bytes1, &n1);
  err = futhark_context_sync(ctx);
  assert(err == 0);

  assert(memcmp(bytes0, bytes1, n0) == 0);

  err = futhark_free_opaque_whatever(ctx, whatever);
  assert(err == 0);

  whatever = futhark_restore_opaque_whatever(ctx, bytes0);
  assert(whatever != NULL);

  struct futhark_i64_1d *out0;
  struct futhark_bool_1d *out1;
  bool out2;

  err = futhark_entry_unmk(ctx, &out0, &out1, &out2, whatever);
  assert(err == 0);
  assert(out2 == 1);

  free(bytes0);
  free(bytes1);

  err = futhark_free_i64_1d(ctx, out0);
  assert(err == 0);

  err = futhark_free_bool_1d(ctx, out1);
  assert(err == 0);

  // Test that passing in garbage fails predictably.
  bytes1 = calloc(n0, 1);
  whatever = futhark_restore_opaque_whatever(ctx, bytes1);
  assert(whatever == NULL);
  free(bytes1);

  futhark_context_free(ctx);
  futhark_context_config_free(cfg);
}