File: test_misc.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 (152 lines) | stat: -rw-r--r-- 4,165 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
143
144
145
146
147
148
149
150
151
152
/*
 * test_misc.c
 *
 * Testing small helper functions.
 *
 * Copyright (C) 2011  Martin Albrecht <martinralbrecht@googlemail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdarg.h>
#include <m4ri/m4ri.h>
#include "testing.h"

#define b(n) (m4ri_one<<(n))

int test_spread_and_shrink(const word to, const int length, ...) {
  word from = 0xFF;
  rci_t *Q = (rci_t*)calloc(sizeof(rci_t),length);
  va_list l;
  va_start(l,length);
  for(size_t i=0; i<length; i++) {
    Q[i] = va_arg(l, size_t);
  }

  const word res = m4ri_spread_bits(from, Q, length, 0);
  const word pre = m4ri_shrink_bits(res, Q, length, 0);

  free(Q);
  va_end(l);

  if (pre != (b(length)-1)) {
    return 1;
  }
  if (res != to) {
    return 1;
  }
  return 0;
}

int test_png(rci_t m, rci_t n) {
  int ret = 0;
#if __M4RI_HAVE_LIBPNG
  printf("png: m: %4d, n: %4d", m, n);

  const char *fn = "test_misc__test_png.png";

  mzd_t *A = mzd_init(m,n);
  mzd_randomize(A);
  mzd_to_png(A, fn, 0, NULL, 0);
  mzd_t *B = mzd_from_png(fn, 0);

  ret += mzd_cmp(A,B);

  remove(fn);
  mzd_free(B);
  mzd_free(A);

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

int test_submatrix(const rci_t m, const rci_t n, const rci_t lowr, const rci_t lowc, const rci_t highr, const rci_t highc) {
  printf("submatrix: m: %4d, n: %4d, (%4d, %4d, %4d, %4d)", m, n, lowr, lowc, highr, highc);
  assert(highr-lowr > 0);
  assert(highc-lowc > 0);
  mzd_t *M = mzd_init(m, n);
  mzd_randomize(M);
  mzd_t *S = mzd_init(highr-lowr, highc-lowc);
  mzd_submatrix(S, M, lowr, lowc, highr, highc);
  int ret = 0;
  for(rci_t i=0; i<highr-lowr; i++) {
    for(rci_t j=0; j<highc-lowc; j++) {
      ret += (mzd_read_bit(M, lowr+i, lowc+j) ^ mzd_read_bit(S, i, j));
    }
  }

  mzd_free(M);
  mzd_free(S);

  if(ret==0) {
    printf(" ... passed\n");
  } else {
    printf(" ... FAILED\n");
  }
  return ret;
}

int main(int argc, char *argv[]) {
  int status = 0;

  status += test_spread_and_shrink( b(1)|b(0), 2, 0,1);
  status += test_spread_and_shrink( b(2)|b(0), 2, 0,2);
  status += test_spread_and_shrink( b(3)|b(1), 2, 1,3);
  status += test_spread_and_shrink( b(3)|b(2), 2, 2,3);
  status += test_spread_and_shrink( b(2)|b(1)|b(0), 3, 0,1,2);
  status += test_spread_and_shrink( b(3)|b(2)|b(0), 3, 0,2,3);
  status += test_spread_and_shrink( b(4)|b(3)|b(1), 3, 1,3,4);
  status += test_spread_and_shrink( b(5)|b(3)|b(2), 3, 2,3,5);

  status += test_png(1,1);
  status += test_png(16,15);
  status += test_png(32,32);
  status += test_png(63,63);
  status += test_png(64,64);
  status += test_png(113,114);
  status += test_png(125,102);
  status += test_png(126,12);
  status += test_png(128,200);

  status += test_submatrix(2, 127, 1, 1, 2, 127);
  status += test_submatrix(2, 128, 1, 1, 2, 128);
  status += test_submatrix(2, 129, 1, 1, 2, 129);
  status += test_submatrix(2, 130, 1, 1, 2, 130);
  status += test_submatrix(2, 131, 1, 1, 2, 131);

  status += test_submatrix(2, 63, 1, 1, 1, 63);
  status += test_submatrix(2, 64, 1, 1, 1, 64);
  status += test_submatrix(2, 65, 1, 1, 1, 65);
  status += test_submatrix(2, 66, 1, 1, 1, 66);
  status += test_submatrix(2, 67, 1, 1, 1, 67);

  status += test_submatrix(2, 127, 1, 63, 2, 127);
  status += test_submatrix(2, 128, 1, 64, 2, 128);
  status += test_submatrix(2, 129, 1, 65, 2, 129);
  status += test_submatrix(2, 130, 1, 66, 2, 130);

  if (!status) {
    printf("All tests passed.\n");
  } else {
    printf("TEST FAILED!\n");
    return 1;
  }
  return 0;
}