File: test_smallops.c

package info (click to toggle)
libm4ri 20200125-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,560 kB
  • sloc: ansic: 12,633; sh: 4,304; makefile: 137
file content (142 lines) | stat: -rw-r--r-- 3,182 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
#include <m4ri/config.h>
#include <stdlib.h>
#include <inttypes.h>
#include "testing.h"
#include <m4ri/m4ri.h>
#include "testing.h"

//#define ABORT_ON_FAIL 1

int smallops_test_add(rci_t M, rci_t N, rci_t m, rci_t n, word pattern) {
  int ret = 0; 

  printf("      mzd_add: M: %4d, N: %4d, m: %4d, n: %4d, pattern: 0x%" PRIx64 " ", M, N, m, n, pattern);

  mzd_t *AA;
  mzd_t *A = mzd_init_test_matrix_random(M, N, m, n, pattern, &AA);

  mzd_t *BB;
  mzd_t *B = mzd_init_test_matrix_random(M, N, m, n, pattern, &BB);

  mzd_t *CC;
  mzd_t *C = mzd_init_test_matrix_random(M, N, m, n, pattern, &CC);

  mzd_t *DD;
  mzd_t *D = mzd_init_test_matrix_random(M, N, m, n, pattern, &DD);

  /* Creation went okay? */

  ret += mzd_check_pattern(AA, m, n, pattern);
  ret += mzd_check_pattern(BB, m, n, pattern);
  ret += mzd_check_pattern(CC, m, n, pattern);
  ret += mzd_check_pattern(DD, m, n, pattern);


  /* Testing equality A+A == 0 */

  mzd_add(C, A, A);

  if(!mzd_is_zero(C)) {
    ret +=1;
  }

  ret += mzd_check_pattern(AA, m, n, pattern);
  ret += mzd_check_pattern(BB, m, n, pattern);
  ret += mzd_check_pattern(CC, m, n, pattern);

  /* Testing equality A+A == 0 but this time C is already zero */

  mzd_add(C, B, B);

  if(!mzd_is_zero(C)) {
    ret +=1;
  }

  ret += mzd_check_pattern(AA, m, n, pattern);
  ret += mzd_check_pattern(BB, m, n, pattern);
  ret += mzd_check_pattern(CC, m, n, pattern);

  /* Testing in place add. C is zero, so afterwards C == A */

  mzd_add(C, C, A);

  if(!mzd_equal(C,A)) {
    ret +=1;
  }

  ret += mzd_check_pattern(AA, m, n, pattern);
  ret += mzd_check_pattern(BB, m, n, pattern);
  ret += mzd_check_pattern(CC, m, n, pattern);

  /* Testing equality C (== A) + A == 0 */

  mzd_add(B, C, A);

  if(!mzd_is_zero(B)) {
    ret +=1;
  }


  if(m == n) {
    /* Testing equality (A + B)^2 == A^2 + BA + AB + B^2 */

    mzd_randomize(A);
    mzd_randomize(B);
    
    mzd_add(C,A,B);
    
    mzd_mul(D,C,C, 0); // (A+B)^2
    
    mzd_mul(C,A,A, 0); 
    mzd_addmul(C, B, A, 0);
    mzd_addmul(C, A, B, 0);
    mzd_addmul(C, B, B, 0);
    
    if(!mzd_equal(C,D)) {
      ret += 1;
    }

    ret += mzd_check_pattern(AA, m, n, pattern);
    ret += mzd_check_pattern(BB, m, n, pattern);
    ret += mzd_check_pattern(CC, m, n, pattern);
    ret += mzd_check_pattern(DD, m, n, pattern);
  }

  mzd_free_test_matrix_random(AA, A);
  mzd_free_test_matrix_random(BB, B);
  mzd_free_test_matrix_random(CC, C);
  mzd_free_test_matrix_random(DD, D);

  if(ret == 0) {
    printf(" ... passed\n");
  } else {
    printf(" ... FAILED\n");
  }
#ifdef ABORT_ON_FAIL
  if (ret) abort();
#endif

  return ret;
}


int main() {
  int status = 0;

  srandom(17);

  status += smallops_test_add( 64,  64,  10,  10, 0x03030303030303llu);
  status += smallops_test_add(100, 100,  64,  64, 0x03030303030303llu);

  status += smallops_test_add(1024, 1024, 513,    511, 0x03030303030303llu);
  status += smallops_test_add(1024, 1024, 512, 768+30, 0x03030303030303llu);

  status += smallops_test_add(2048, 2048, 1024, 1024,  0x03030303030303llu);

  if (status == 0) {
    printf("All tests passed.\n");
    return 0;
  } else {
    return -1;
  }
}