1 // Copyright 2017 The Bazel Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "ffi/rust_calling_c/c/matrix.h"
16
17 #include <assert.h>
18 #include <inttypes.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
matrix_print(const Matrix * m)23 void matrix_print(const Matrix* m) {
24 for (size_t i = 0; i < m->rows; ++i) {
25 for (size_t j = 0; j < m->cols; ++j) {
26 uint64_t val = 0;
27 matrix_at(m, i, j, &val);
28 printf("%" PRIu64 " ", val);
29 }
30 printf("\n");
31 }
32 }
33
check_equal(const Matrix * a,const Matrix * b)34 int check_equal(const Matrix* a, const Matrix* b) {
35 int equal = matrix_equal(a, b);
36 if (!equal) {
37 printf("Matrices not equal:\n");
38 printf("a:\n");
39 matrix_print(a);
40 printf("\nb:\n");
41 matrix_print(b);
42 }
43 return equal;
44 }
45
test_equal()46 void test_equal() {
47 // clang-format off
48 static uint64_t a_data[] = {11, 12, 13, 14,
49 21, 22, 23, 24};
50 // clang-format on
51 Matrix* a = matrix_new(2, 4, a_data);
52 assert(a != NULL);
53 assert(check_equal(a, a));
54
55 // clang-format off
56 static uint64_t b_data[] = {13, 14, 15, 16,
57 22, 23, 24, 25};
58 // clang-format on
59 Matrix* b = matrix_new(2, 4, b_data);
60 assert(b != NULL);
61 assert(!matrix_equal(a, b));
62 }
63
test_transpose()64 void test_transpose() {
65 // clang-format off
66 static uint64_t matrix_data[] = {11, 12, 13, 14,
67 21, 22, 23, 24};
68 // clang-format on
69 Matrix* matrix = matrix_new(2, 4, matrix_data);
70 assert(matrix != NULL);
71 matrix_transpose(matrix);
72
73 // clang-format off
74 static uint64_t expected_transpose_data[] = {11, 21,
75 12, 22,
76 13, 23,
77 14, 24};
78 // clang-format off
79 Matrix* expected_transpose = matrix_new(4, 2, expected_transpose_data);
80
81 assert(check_equal(expected_transpose, matrix));
82 }
83
main(int argc,char ** argv)84 int main(int argc, char** argv) {
85 test_equal();
86 test_transpose();
87 return EXIT_SUCCESS;
88 }
89