[421b7d2] | 1 | /* validate.c |
---|
[d1b1380] | 2 | * |
---|
| 3 | * Checks that SURVEX's data structures are valid and consistent |
---|
| 4 | * |
---|
| 5 | * NB The checks currently done aren't very comprehensive - more will be |
---|
| 6 | * added if bugs require them |
---|
| 7 | * |
---|
[b6e9e7e] | 8 | * Copyright (C) 1993,1994,1996,2000,2001 Olly Betts |
---|
[846746e] | 9 | * |
---|
[89231c4] | 10 | * This program is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | * (at your option) any later version. |
---|
[846746e] | 14 | * |
---|
| 15 | * This program is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | * GNU General Public License for more details. |
---|
[846746e] | 19 | * |
---|
[89231c4] | 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 23 | */ |
---|
| 24 | |
---|
[4c83f84] | 25 | #include <config.h> |
---|
[d1b1380] | 26 | |
---|
[a420b49] | 27 | #include "cavern.h" |
---|
[c082b69] | 28 | #include "filename.h" |
---|
| 29 | #include "message.h" |
---|
[d1b1380] | 30 | #include "netbits.h" |
---|
| 31 | #include "validate.h" |
---|
| 32 | |
---|
| 33 | /* maximum absolute value allowed for a coordinate of a fixed station */ |
---|
[577d6c1] | 34 | #define MAX_POS 10000000.0 |
---|
[d1b1380] | 35 | |
---|
[a420b49] | 36 | static bool validate_prefix_tree(void); |
---|
| 37 | static bool validate_prefix_subtree(prefix *pfx); |
---|
[d1b1380] | 38 | |
---|
[a420b49] | 39 | static bool validate_station_list(void); |
---|
[d1b1380] | 40 | |
---|
| 41 | #if 0 |
---|
[a420b49] | 42 | extern void |
---|
| 43 | check_fixed(void) |
---|
| 44 | { |
---|
| 45 | /* not a requirement -- we allow hanging sections of survey |
---|
| 46 | * which get spotted and removed */ |
---|
| 47 | node *stn; |
---|
| 48 | printf("*** Checking fixed-ness\n"); |
---|
[6d3f931] | 49 | /* NB: don't use FOR_EACH_STN as it isn't reentrant at present */ |
---|
| 50 | for (stn = stnlist; stn; stn = stn->next) { |
---|
[a420b49] | 51 | if (stn->status && !fixed(stn)) { |
---|
| 52 | printf("*** Station '"); |
---|
| 53 | print_prefix(stn->name); |
---|
| 54 | printf("' has status %d but isn't fixed\n", stn->status); |
---|
| 55 | } |
---|
| 56 | } |
---|
[d1b1380] | 57 | } |
---|
| 58 | #endif |
---|
| 59 | |
---|
[a420b49] | 60 | #undef validate |
---|
| 61 | extern bool |
---|
| 62 | validate(void) |
---|
| 63 | { |
---|
[63d4f07] | 64 | bool fOk = true; |
---|
| 65 | if (!validate_prefix_tree()) fOk = false; |
---|
| 66 | if (!validate_station_list()) fOk = false; |
---|
[a420b49] | 67 | if (fOk) puts("*** Data structures passed consistency checks"); |
---|
[6d3f931] | 68 | else puts("*** Data structures FAILED consistency checks"); |
---|
[a420b49] | 69 | return fOk; |
---|
| 70 | } |
---|
[d1b1380] | 71 | |
---|
[a420b49] | 72 | static bool |
---|
| 73 | validate_prefix_tree(void) |
---|
| 74 | { |
---|
[63d4f07] | 75 | bool fOk = true; |
---|
[a420b49] | 76 | if (root->up != NULL) { |
---|
| 77 | printf("*** root->up == %p\n", root->up); |
---|
[63d4f07] | 78 | fOk = false; |
---|
[a420b49] | 79 | } |
---|
| 80 | if (root->right != NULL) { |
---|
| 81 | printf("*** root->right == %p\n", root->right); |
---|
[63d4f07] | 82 | fOk = false; |
---|
[a420b49] | 83 | } |
---|
| 84 | if (root->stn != NULL) { |
---|
| 85 | printf("*** root->stn == %p\n", root->stn); |
---|
[63d4f07] | 86 | fOk = false; |
---|
[a420b49] | 87 | } |
---|
| 88 | if (root->pos != NULL) { |
---|
| 89 | printf("*** root->pos == %p\n", root->pos); |
---|
[63d4f07] | 90 | fOk = false; |
---|
[a420b49] | 91 | } |
---|
| 92 | fOk &= validate_prefix_subtree(root); |
---|
| 93 | return fOk; |
---|
| 94 | } |
---|
[d1b1380] | 95 | |
---|
[a420b49] | 96 | static bool |
---|
| 97 | validate_prefix_subtree(prefix *pfx) |
---|
| 98 | { |
---|
[63d4f07] | 99 | bool fOk = true; |
---|
[a420b49] | 100 | prefix *pfx2; |
---|
| 101 | pfx2 = pfx->down; |
---|
| 102 | /* this happens now, as nodes are freed after solving */ |
---|
[d1b1380] | 103 | #if 0 |
---|
[a420b49] | 104 | if (pfx2 == NULL) { |
---|
| 105 | if (pfx->stn == NULL) { |
---|
| 106 | printf("*** Leaf prefix '"); |
---|
| 107 | print_prefix(pfx); |
---|
| 108 | printf("' has no station attached\n"); |
---|
[63d4f07] | 109 | fOk = false; |
---|
[a420b49] | 110 | } |
---|
| 111 | return fOk; |
---|
[d1b1380] | 112 | } |
---|
| 113 | #endif |
---|
| 114 | |
---|
[a420b49] | 115 | while (pfx2 != NULL) { |
---|
| 116 | if (pfx2->stn != NULL && pfx2->stn->name != pfx2) { |
---|
| 117 | printf("*** Prefix '"); |
---|
| 118 | print_prefix(pfx2); |
---|
| 119 | printf("' ->stn->name is '"); |
---|
| 120 | print_prefix(pfx2->stn->name); |
---|
| 121 | printf("'\n"); |
---|
[63d4f07] | 122 | fOk = false; |
---|
[a420b49] | 123 | } |
---|
| 124 | if (pfx2->up != pfx) { |
---|
| 125 | printf("*** Prefix '"); |
---|
| 126 | print_prefix(pfx2); |
---|
| 127 | printf("' ->up is '"); |
---|
| 128 | print_prefix(pfx); |
---|
| 129 | printf("'\n"); |
---|
[63d4f07] | 130 | fOk = false; |
---|
[a420b49] | 131 | } |
---|
| 132 | fOk &= validate_prefix_subtree(pfx2); |
---|
| 133 | pfx2 = pfx2->right; |
---|
[d1b1380] | 134 | } |
---|
[a420b49] | 135 | return fOk; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | static bool |
---|
| 139 | validate_station_list(void) |
---|
| 140 | { |
---|
[63d4f07] | 141 | bool fOk = true; |
---|
[a420b49] | 142 | node *stn, *stn2; |
---|
| 143 | int d, d2; |
---|
| 144 | |
---|
[4c07c51] | 145 | SVX_ASSERT(!stnlist || !stnlist->prev); |
---|
[6d3f931] | 146 | /* NB: don't use FOR_EACH_STN as it isn't reentrant at present */ |
---|
| 147 | for (stn = stnlist; stn; stn = stn->next) { |
---|
[63d4f07] | 148 | bool fGap = false; |
---|
[095ef764] | 149 | #if 0 |
---|
| 150 | printf("V [%p]<-[%p]->[%p] ", stn->prev, stn, stn->next); print_prefix(stn->name); putnl(); |
---|
| 151 | #endif |
---|
[4c07c51] | 152 | SVX_ASSERT(stn->prev == NULL || stn->prev->next == stn); |
---|
| 153 | SVX_ASSERT(stn->next == NULL || stn->next->prev == stn); |
---|
[a420b49] | 154 | for (d = 0; d <= 2; d++) { |
---|
[577d6c1] | 155 | if (!stn->leg[d]) { |
---|
[63d4f07] | 156 | fGap = true; |
---|
[577d6c1] | 157 | } else { |
---|
| 158 | if (fGap) { |
---|
| 159 | printf("*** Station '"); |
---|
| 160 | print_prefix(stn->name); |
---|
| 161 | printf("', leg %d is used, but an earlier leg isn't\n", d); |
---|
[63d4f07] | 162 | fOk = false; |
---|
[577d6c1] | 163 | } |
---|
[a420b49] | 164 | stn2 = stn->leg[d]->l.to; |
---|
[4c07c51] | 165 | SVX_ASSERT(stn2); |
---|
[564f471] | 166 | #if 0 |
---|
[a420b49] | 167 | if (stn->status && !stn2->status) { |
---|
| 168 | printf("*** Station '"); |
---|
| 169 | print_prefix(stn->name); |
---|
| 170 | printf("' has status %d and connects to '", stn->status); |
---|
| 171 | print_prefix(stn2->name); |
---|
| 172 | printf("' which has status %d\n", stn2->status); |
---|
[63d4f07] | 173 | fOk = false; |
---|
[a420b49] | 174 | } |
---|
[564f471] | 175 | #endif |
---|
[a420b49] | 176 | d2 = reverse_leg_dirn(stn->leg[d]); |
---|
| 177 | if (stn2->leg[d2] == NULL) { |
---|
| 178 | /* fine iff stn is at the disconnected end of a fragment */ |
---|
[564f471] | 179 | node *s; |
---|
[6d3f931] | 180 | /* NB: don't use FOR_EACH_STN as it isn't reentrant at present */ |
---|
| 181 | for (s = stnlist; s; s = s->next) if (s == stn) break; |
---|
[564f471] | 182 | if (s) { |
---|
[a420b49] | 183 | printf("*** Station '"); |
---|
| 184 | print_prefix(stn->name); |
---|
| 185 | printf("', leg %d doesn't reciprocate from station '", d); |
---|
| 186 | print_prefix(stn2->name); |
---|
| 187 | printf("'\n"); |
---|
[63d4f07] | 188 | fOk = false; |
---|
[a420b49] | 189 | } |
---|
[6d3f931] | 190 | } else if (stn2->leg[d2]->l.to == NULL) { |
---|
| 191 | printf("*** Station '"); |
---|
| 192 | print_prefix(stn2->name); |
---|
| 193 | printf("' [%p], leg %d points to NULL\n", stn2, d2); |
---|
[63d4f07] | 194 | fOk = false; |
---|
[a420b49] | 195 | } else if (stn2->leg[d2]->l.to!=stn) { |
---|
| 196 | /* fine iff stn is at the disconnected end of a fragment */ |
---|
[564f471] | 197 | node *s; |
---|
[6d3f931] | 198 | /* NB: don't use FOR_EACH_STN as it isn't reentrant at present */ |
---|
| 199 | for (s = stnlist; s; s = s->next) if (s == stn) break; |
---|
[564f471] | 200 | if (s) { |
---|
[a420b49] | 201 | printf("*** Station '"); |
---|
| 202 | print_prefix(stn->name); |
---|
[6d3f931] | 203 | printf("' [%p], leg %d reciprocates via station '", stn, d); |
---|
[a420b49] | 204 | print_prefix(stn2->name); |
---|
| 205 | printf("' to station '"); |
---|
| 206 | print_prefix(stn2->leg[d2]->l.to->name); |
---|
| 207 | printf("'\n"); |
---|
[63d4f07] | 208 | fOk = false; |
---|
[a420b49] | 209 | } |
---|
| 210 | } else if ((data_here(stn->leg[d]) != 0) ^ |
---|
| 211 | (data_here(stn2->leg[d2]) == 0)) { |
---|
| 212 | printf("*** Station '"); |
---|
| 213 | print_prefix(stn->name); |
---|
[6d3f931] | 214 | printf("' [%p], leg %d reciprocates via station '", stn, d); |
---|
[a420b49] | 215 | print_prefix(stn2->name); |
---|
[cb3d1e2] | 216 | if (data_here(stn->leg[d])) |
---|
[a420b49] | 217 | printf("' - data on both legs\n"); |
---|
| 218 | else |
---|
| 219 | printf("' - data on neither leg\n"); |
---|
[63d4f07] | 220 | fOk = false; |
---|
[a420b49] | 221 | } |
---|
| 222 | if (data_here(stn->leg[d])) { |
---|
| 223 | int i; |
---|
| 224 | for (i = 0; i < 3; i++) |
---|
| 225 | if (fabs(stn->leg[d]->d[i]) > MAX_POS) { |
---|
| 226 | printf("*** Station '"); |
---|
| 227 | print_prefix(stn->name); |
---|
| 228 | printf("', leg %d, d[%d] = %g\n", |
---|
| 229 | d, i, (double)(stn->leg[d]->d[i])); |
---|
[63d4f07] | 230 | fOk = false; |
---|
[a420b49] | 231 | } |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | if (fixed(stn)) { |
---|
| 236 | if (fabs(POS(stn, 0)) > MAX_POS || |
---|
| 237 | fabs(POS(stn, 1)) > MAX_POS || |
---|
| 238 | fabs(POS(stn, 2)) > MAX_POS) { |
---|
| 239 | printf("*** Station '"); |
---|
| 240 | print_prefix(stn->name); |
---|
| 241 | printf("' fixed at coords (%f,%f,%f)\n", |
---|
| 242 | POS(stn, 0), POS(stn, 1), POS(stn, 2) ); |
---|
[63d4f07] | 243 | fOk = false; |
---|
[a420b49] | 244 | } |
---|
| 245 | } |
---|
[d1b1380] | 246 | |
---|
| 247 | } |
---|
[a420b49] | 248 | } |
---|
| 249 | return fOk; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | #undef dump_node |
---|
| 253 | extern void |
---|
| 254 | dump_node(node *stn) |
---|
| 255 | { |
---|
| 256 | int d; |
---|
| 257 | if (stn->name) |
---|
[d1b1380] | 258 | print_prefix(stn->name); |
---|
[a420b49] | 259 | else |
---|
| 260 | printf("<null>"); |
---|
[d1b1380] | 261 | |
---|
[564f471] | 262 | printf(" stn [%p] name (%p) colour %ld %sfixed\n", |
---|
| 263 | stn, stn->name, stn->colour, fixed(stn) ? "" : "un"); |
---|
[d1b1380] | 264 | |
---|
[a420b49] | 265 | for (d = 0; d <= 2; d++) { |
---|
[05312022] | 266 | if (stn->leg[d]) { |
---|
[6d3f931] | 267 | printf(" leg %d -> stn [%p] rev %d ", d, stn->leg[d]->l.to, |
---|
| 268 | reverse_leg_dirn(stn->leg[d])); |
---|
[05312022] | 269 | print_prefix(stn->leg[d]->l.to->name); |
---|
[5b68ae1] | 270 | putnl(); |
---|
[05312022] | 271 | } |
---|
[d1b1380] | 272 | } |
---|
[a420b49] | 273 | } |
---|
[d1b1380] | 274 | |
---|
[b6e9e7e] | 275 | /* This doesn't cover removed stations - might be nice to have |
---|
| 276 | * dump_entire_network() which iterates prefix tree */ |
---|
[cb3d1e2] | 277 | #undef dump_network |
---|
[a420b49] | 278 | extern void |
---|
| 279 | dump_network(void) |
---|
| 280 | { |
---|
| 281 | node *stn; |
---|
[6d3f931] | 282 | /* NB: don't use FOR_EACH_STN as it isn't reentrant at present */ |
---|
| 283 | for (stn = stnlist; stn; stn = stn->next) dump_node(stn); |
---|
[a420b49] | 284 | } |
---|