1 #include "tests/helpers.h"
2 #include "sources/neighbors.h"
5 int** list2int(List
** L
, int n
)
7 int** I
= (int**)malloc(n
*sizeof(int*));
8 for (int i
=0; i
<n
; i
++)
10 int listSize
= list_size(L
[i
]);
11 I
[i
] = (int*)malloc((1+listSize
)*sizeof(int));
13 ListIterator
* iterJ
= list_get_iterator(L
[i
]);
14 for (int j
=1; j
<=listSize
; j
++)
16 listI_get(iterJ
, I
[i
][j
]);
17 listI_move_next(iterJ
);
24 //10 lines 12 columns, only NA except on antidiagonal
25 // ==> distances computed with coordinates only
26 void test_neighbors1()
31 NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,1.0,0.0,0.0,
32 NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,1.0,NAN
,1.0,1.0,
33 NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,1.0,NAN
,NAN
,2.0,2.0,
34 NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,1.0,NAN
,NAN
,NAN
,3.0,3.0,
35 NAN
,NAN
,NAN
,NAN
,NAN
,1.0,NAN
,NAN
,NAN
,NAN
,4.0,4.0,
36 NAN
,NAN
,NAN
,NAN
,1.0,NAN
,NAN
,NAN
,NAN
,NAN
,5.0,5.0,
37 NAN
,NAN
,NAN
,1.0,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,6.0,6.0,
38 NAN
,NAN
,1.0,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,7.0,7.0,
39 NAN
,1.0,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,8.0,8.0,
40 1.0,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,NAN
,9.0,9.0
43 double alphas
[4] = {-1.0, 0.0, 0.5, 1.0};
44 int k
= 2; // no need for more
45 for (int j
=0; j
<4; j
++)
47 double alpha
= alphas
[j
]; // no impact
48 for (int gmode
=0; gmode
<4; gmode
++)
50 List
** L
= getNeighbors_core(M
, alpha
, k
, gmode
, S_FALSE
, n
, m
);
51 int** NIix
= list2int(L
, n
);
52 for (int jj
=0; jj
<n
; jj
++)
55 for (int jj
=1; jj
<n
-1; jj
++)
57 // interior points: 2 neighbors, below and above [except for gmode==1 and jj==2 or n-2]
58 if (gmode
==1 && (jj
==2 || jj
==n
-3))
61 NIix
[jj
][0] == 3 && (NIix
[jj
][1] == jj
-1 || NIix
[jj
][2] == jj
-1 || NIix
[jj
][3] == jj
-1));
63 NIix
[jj
][0] == 3 && (NIix
[jj
][1] == jj
+1 || NIix
[jj
][2] == jj
+1 || NIix
[jj
][3] == jj
+1));
67 NIix
[jj
][0] == 3 && (NIix
[jj
][1] == jj
-2 || NIix
[jj
][2] == jj
-2 || NIix
[jj
][3] == jj
-2));
72 NIix
[jj
][0] == 3 && (NIix
[jj
][1] == jj
+2 || NIix
[jj
][2] == jj
+2 || NIix
[jj
][3] == jj
+2));
77 // one neighb below, one neighb above
79 NIix
[jj
][0] == 2 && (NIix
[jj
][1] == jj
-1 || NIix
[jj
][2] == jj
-1));
81 NIix
[jj
][0] == 2 && (NIix
[jj
][1] == jj
+1 || NIix
[jj
][2] == jj
+1));
84 // boundary points in mode 1 (augmented kNN) or 2 (normal kNN) also have 2 neighbors
85 if (gmode
==1 || gmode
==2)
87 ASSERT_TRUE(NIix
[0][0] == 2 && (NIix
[0][1] == 1 || NIix
[0][2] == 1));
88 ASSERT_TRUE(NIix
[0][0] == 2 && (NIix
[0][1] == 2 || NIix
[0][2] == 2));
89 ASSERT_TRUE(NIix
[n
-1][0] == 2 && (NIix
[n
-1][1] == n
-2 || NIix
[n
-1][2] == n
-2));
90 ASSERT_TRUE(NIix
[n
-1][0] == 2 && (NIix
[n
-1][1] == n
-3 || NIix
[n
-1][2] == n
-3));
94 // in mutual kNN or in 'by-quadrants' modes, they only have 1 neighbor.
95 ASSERT_TRUE(NIix
[0][0] == 1 && NIix
[0][1] == 1);
96 ASSERT_TRUE(NIix
[n
-1][0] == 1 && NIix
[n
-1][1] == n
-2);
99 for (int i
=0; i
<n
; i
++)
106 //10 lines, 6 columns, some NA's...
107 void test_neighbors2()
112 NAN
,1.0,0.0,NAN
,0.0,NAN
,3.0,NAN
,0.0,0.0,
113 1.0,2.0,0.0,NAN
,NAN
,0.0,2.0,3.0,1.0,1.0,
114 2.0,3.0,0.0,NAN
,NAN
,1.0,1.0,2.0,2.0,2.0,
115 3.0,NAN
,0.0,NAN
,1.0,NAN
,NAN
,1.0,3.0,3.0,
116 2.0,3.0,NAN
,3.0,1.0,2.0,1.0,NAN
,4.0,4.0,
117 1.0,0.0,NAN
,2.0,NAN
,1.0,3.0,NAN
,5.0,5.0
120 List
** L
= getNeighbors_core(M
, 0.5, 3, 2, S_FALSE
, n
, m
);
121 int** NIix
= list2int(L
, n
);
122 for (int j
=0; j
<n
; j
++)
126 // check neighbors of first row
127 ASSERT_TRUE(NIix
[0][0] == 3);
128 for (int j
=1; j
<=3; j
++)
130 ASSERT_TRUE(NIix
[0][1] == j
||
131 NIix
[0][2] == j
|| NIix
[0][3] == j
);
133 for (int i
=0; i
<n
; i
++)
137 L
= getNeighbors_core(M
, -1.0, 3, 2, S_FALSE
, n
, m
);
138 NIix
= list2int(L
, n
);
139 for (int j
=0; j
<n
; j
++)
143 // check neighbors of fifth row
144 ASSERT_TRUE(NIix
[4][0] == 3);
145 for (int j
=2; j
<=5; j
++)
148 continue; //not self-neighbor
149 ASSERT_TRUE(NIix
[4][1] == j
||
150 NIix
[4][2] == j
|| NIix
[4][3] == j
);
153 for (int i
=0; i
<n
; i
++)