| 1 | #include "tests/helpers.h" |
| 2 | #include "sources/connexity.h" |
| 3 | |
| 4 | //completely disconnected graph (no edges) |
| 5 | void test_connexity1() |
| 6 | { |
| 7 | int n = 10; |
| 8 | int* lengthNIix = (int*)calloc(n,sizeof(int)); |
| 9 | int** NIix = (int**)malloc(n*sizeof(int*)); |
| 10 | for (int i=0; i<n; i++) NIix[i] = NULL; |
| 11 | int* cc = getConnectedComponents_core(NIix, lengthNIix, n); |
| 12 | //cc should contain all integers from 1 to n |
| 13 | ASSERT_TRUE(countDistinctValues(cc,n) == n); |
| 14 | free(lengthNIix); |
| 15 | free(NIix); |
| 16 | free(cc); |
| 17 | } |
| 18 | |
| 19 | //bipartite graph |
| 20 | void test_connexity2() |
| 21 | { |
| 22 | int n = 10; |
| 23 | int* lengthNIix = (int*)malloc(n*sizeof(int)); |
| 24 | int** NIix = (int**)malloc(n*sizeof(int*)); |
| 25 | for (int i=0; i<n; i++) |
| 26 | { |
| 27 | lengthNIix[i] = 1; |
| 28 | NIix[i] = (int*)malloc(sizeof(int)); |
| 29 | } |
| 30 | // connect 0 with 1, 2 with 3 ... |
| 31 | for (int i=0; i<n; i+=2) |
| 32 | { |
| 33 | NIix[i][0] = i+1; |
| 34 | NIix[i+1][0] = i; |
| 35 | } |
| 36 | int* cc = getConnectedComponents_core(NIix, lengthNIix, n); |
| 37 | //cc should contain exactly n/2 integers |
| 38 | ASSERT_TRUE(countDistinctValues(cc,n) == n/2); |
| 39 | free(lengthNIix); |
| 40 | for (int i=0; i<n; i++) |
| 41 | free(NIix[i]); |
| 42 | free(NIix); |
| 43 | free(cc); |
| 44 | } |
| 45 | |
| 46 | //~ //cyclic graph |
| 47 | //~ void test_connexity3() { |
| 48 | //~ int n = 10; |
| 49 | //~ int* adjMat = (int*)calloc(n*n,sizeof(int)); |
| 50 | //~ // connect 0 with 1, 1 with 2 ... |
| 51 | //~ for (int i=0; i<n; i++) { |
| 52 | //~ adjMat[i+n*((i+1)%n)] = TRUE; |
| 53 | //~ adjMat[(i+1)%n+n*i] = TRUE; |
| 54 | //~ } |
| 55 | //~ int* cc = getConnectedComponents_core(adjMat, n); |
| 56 | //~ //cc should contain only one value (presumably '1') |
| 57 | //~ warnIfFails(countDistinctValues(cc,n) == 1, "c3", ""); |
| 58 | //~ free(adjMat); |
| 59 | //~ free(cc); |
| 60 | //~ } |
| 61 | //~ |
| 62 | //~ //custom graph with 3 connex components |
| 63 | //~ void test_connexity4() { |
| 64 | //~ int n = 10; |
| 65 | //~ |
| 66 | //~ |
| 67 | //~ {2,4}, |
| 68 | //~ {2,4}, |
| 69 | //~ {0,1}, |
| 70 | //~ {5,8,9}, |
| 71 | //~ {0,1}, |
| 72 | //~ {3,7}, |
| 73 | //~ {}, |
| 74 | //~ {5,9}, |
| 75 | //~ {3}, |
| 76 | //~ {3,7} |
| 77 | //~ |
| 78 | //~ int adjMat[100] = |
| 79 | //~ { |
| 80 | //~ 0,0,1,0,1,0,0,0,0,0, |
| 81 | //~ 0,0,1,0,1,0,0,0,0,0, |
| 82 | //~ 1,1,0,0,0,0,0,0,0,0, |
| 83 | //~ 0,0,0,0,0,1,0,0,1,1, |
| 84 | //~ 1,1,0,0,0,0,0,0,0,0, |
| 85 | //~ 0,0,0,1,0,0,0,1,0,0, |
| 86 | //~ 0,0,0,0,0,0,0,0,0,0, |
| 87 | //~ 0,0,0,0,0,1,0,0,0,1, |
| 88 | //~ 0,0,0,1,0,0,0,0,0,0, |
| 89 | //~ 0,0,0,1,0,0,0,1,0,0 |
| 90 | //~ }; |
| 91 | //~ int* cc = getConnectedComponents_core(adjMat, n); |
| 92 | //~ //cc should contain exactly 3 values |
| 93 | //~ warnIfFails(countDistinctValues(cc,n) == 3, "c4", ""); |
| 94 | //~ free(cc); |
| 95 | //~ } |
| 96 | //~ |
| 97 | //~ //custom graph, 1 connex component |
| 98 | //~ void test_connexity5() { |
| 99 | //~ int n = 10; |
| 100 | //~ int adjMat[100] = |
| 101 | //~ { |
| 102 | //~ 0,0,1,1,0,0,0,1,0,0, |
| 103 | //~ 0,0,1,0,1,0,1,0,0,0, |
| 104 | //~ 1,1,0,0,0,0,0,0,0,0, |
| 105 | //~ 1,0,0,0,0,1,0,0,1,1, |
| 106 | //~ 0,1,0,0,0,0,0,0,0,0, |
| 107 | //~ 0,0,0,1,0,0,0,1,0,0, |
| 108 | //~ 0,1,0,0,0,0,0,0,0,0, |
| 109 | //~ 1,0,0,0,0,1,0,0,0,1, |
| 110 | //~ 0,0,0,1,0,0,0,0,0,0, |
| 111 | //~ 0,0,0,1,0,0,0,1,0,0 |
| 112 | //~ }; |
| 113 | //~ int* cc = getConnectedComponents_core(adjMat, n); |
| 114 | //~ //cc should contain only one value (presumably '1') |
| 115 | //~ warnIfFails(countDistinctValues(cc,n) == 1, "c5", ""); |
| 116 | //~ free(cc); |
| 117 | //~ } |