microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc_data_structures/src/namespaces/tests.rs
518lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | // allowing needless raw hashes here because we auto-update these expected outputs |
| 5 | // and don't want to risk weird breakages |
| 6 | |
| 7 | #![allow(clippy::needless_raw_string_hashes)] |
| 8 | |
| 9 | use expect_test::expect; |
| 10 | |
| 11 | use super::*; |
| 12 | |
| 13 | #[allow(clippy::too_many_lines)] |
| 14 | #[test] |
| 15 | fn test_tree_construction() { |
| 16 | let mut root = NamespaceTreeRoot::default(); |
| 17 | for i in 0..3 { |
| 18 | for j in 'a'..'d' { |
| 19 | let _ = root.insert_or_find_namespace( |
| 20 | vec![Rc::from(format!("ns{i}")), Rc::from(format!("ns{j}"))].into_iter(), |
| 21 | ); |
| 22 | } |
| 23 | } |
| 24 | expect![[r#" |
| 25 | NamespaceTreeRoot |
| 26 | |
| 27 | children: [ |
| 28 | ns1(id 11) { |
| 29 | children: [ |
| 30 | nsc(id 14) {empty node}, |
| 31 | nsb(id 13) {empty node}, |
| 32 | nsa(id 12) {empty node}, |
| 33 | ] |
| 34 | }, |
| 35 | ns0(id 7) { |
| 36 | children: [ |
| 37 | nsc(id 10) {empty node}, |
| 38 | nsb(id 9) {empty node}, |
| 39 | nsa(id 8) {empty node}, |
| 40 | ] |
| 41 | }, |
| 42 | Microsoft(id 1) { |
| 43 | children: [ |
| 44 | Quantum(id 2) { |
| 45 | children: [ |
| 46 | Canon(id 3) {empty node}, |
| 47 | Measurement(id 6) {empty node}, |
| 48 | Core(id 4) {empty node}, |
| 49 | Intrinsic(id 5) {empty node}, |
| 50 | ] |
| 51 | }, |
| 52 | ] |
| 53 | }, |
| 54 | ns2(id 15) { |
| 55 | children: [ |
| 56 | nsc(id 18) {empty node}, |
| 57 | nsb(id 17) {empty node}, |
| 58 | nsa(id 16) {empty node}, |
| 59 | ] |
| 60 | }, |
| 61 | ] |
| 62 | } |
| 63 | "#]] |
| 64 | .assert_debug_eq(&root); |
| 65 | } |
| 66 | |
| 67 | #[allow(clippy::too_many_lines)] |
| 68 | #[test] |
| 69 | fn test_find_id() { |
| 70 | let mut root = NamespaceTreeRoot::default(); |
| 71 | let mut id_buf = vec![]; |
| 72 | for i in 0..3 { |
| 73 | for j in 'a'..'d' { |
| 74 | id_buf.push(root.insert_or_find_namespace( |
| 75 | vec![Rc::from(format!("ns{i}")), Rc::from(format!("ns{j}"))].into_iter(), |
| 76 | )); |
| 77 | } |
| 78 | id_buf.push(root.insert_or_find_namespace(vec![Rc::from(format!("ns{i}"))].into_iter())); |
| 79 | } |
| 80 | let mut result_buf = vec![]; |
| 81 | for id in id_buf { |
| 82 | result_buf.push(root.find_namespace_by_id(&id)); |
| 83 | } |
| 84 | expect![[r#" |
| 85 | [ |
| 86 | ( |
| 87 | [ |
| 88 | "ns0", |
| 89 | "nsa", |
| 90 | ], |
| 91 | RefCell { |
| 92 | value: empty node}, |
| 93 | }, |
| 94 | ), |
| 95 | ( |
| 96 | [ |
| 97 | "ns0", |
| 98 | "nsb", |
| 99 | ], |
| 100 | RefCell { |
| 101 | value: empty node}, |
| 102 | }, |
| 103 | ), |
| 104 | ( |
| 105 | [ |
| 106 | "ns0", |
| 107 | "nsc", |
| 108 | ], |
| 109 | RefCell { |
| 110 | value: empty node}, |
| 111 | }, |
| 112 | ), |
| 113 | ( |
| 114 | [ |
| 115 | "ns0", |
| 116 | ], |
| 117 | RefCell { |
| 118 | value: |
| 119 | children: [ |
| 120 | nsc(id 10) {empty node}, |
| 121 | nsb(id 9) {empty node}, |
| 122 | nsa(id 8) {empty node}, |
| 123 | ] |
| 124 | }, |
| 125 | }, |
| 126 | ), |
| 127 | ( |
| 128 | [ |
| 129 | "ns1", |
| 130 | "nsa", |
| 131 | ], |
| 132 | RefCell { |
| 133 | value: empty node}, |
| 134 | }, |
| 135 | ), |
| 136 | ( |
| 137 | [ |
| 138 | "ns1", |
| 139 | "nsb", |
| 140 | ], |
| 141 | RefCell { |
| 142 | value: empty node}, |
| 143 | }, |
| 144 | ), |
| 145 | ( |
| 146 | [ |
| 147 | "ns1", |
| 148 | "nsc", |
| 149 | ], |
| 150 | RefCell { |
| 151 | value: empty node}, |
| 152 | }, |
| 153 | ), |
| 154 | ( |
| 155 | [ |
| 156 | "ns1", |
| 157 | ], |
| 158 | RefCell { |
| 159 | value: |
| 160 | children: [ |
| 161 | nsc(id 14) {empty node}, |
| 162 | nsb(id 13) {empty node}, |
| 163 | nsa(id 12) {empty node}, |
| 164 | ] |
| 165 | }, |
| 166 | }, |
| 167 | ), |
| 168 | ( |
| 169 | [ |
| 170 | "ns2", |
| 171 | "nsa", |
| 172 | ], |
| 173 | RefCell { |
| 174 | value: empty node}, |
| 175 | }, |
| 176 | ), |
| 177 | ( |
| 178 | [ |
| 179 | "ns2", |
| 180 | "nsb", |
| 181 | ], |
| 182 | RefCell { |
| 183 | value: empty node}, |
| 184 | }, |
| 185 | ), |
| 186 | ( |
| 187 | [ |
| 188 | "ns2", |
| 189 | "nsc", |
| 190 | ], |
| 191 | RefCell { |
| 192 | value: empty node}, |
| 193 | }, |
| 194 | ), |
| 195 | ( |
| 196 | [ |
| 197 | "ns2", |
| 198 | ], |
| 199 | RefCell { |
| 200 | value: |
| 201 | children: [ |
| 202 | nsc(id 18) {empty node}, |
| 203 | nsb(id 17) {empty node}, |
| 204 | nsa(id 16) {empty node}, |
| 205 | ] |
| 206 | }, |
| 207 | }, |
| 208 | ), |
| 209 | ] |
| 210 | "#]] |
| 211 | .assert_debug_eq(&result_buf); |
| 212 | } |
| 213 | // test that after inserting lots of namespaces, all ids are unique and sequential |
| 214 | #[test] |
| 215 | fn test_insert_or_find_namespace() { |
| 216 | let mut root = NamespaceTreeRoot::default(); |
| 217 | let mut ids: Vec<usize> = vec![]; |
| 218 | for i in 0..3 { |
| 219 | for j in 'a'..'d' { |
| 220 | let id = root.insert_or_find_namespace( |
| 221 | vec![Rc::from(format!("ns{i}")), Rc::from(format!("ns{j}"))].into_iter(), |
| 222 | ); |
| 223 | ids.push(id.into()); |
| 224 | } |
| 225 | } |
| 226 | let mut ids_sorted = ids.clone(); |
| 227 | ids_sorted.sort_unstable(); |
| 228 | ids_sorted.dedup(); |
| 229 | // there should be no duplicate or out-of-order ids |
| 230 | assert_eq!(ids_sorted, ids); |
| 231 | expect![[r" |
| 232 | [ |
| 233 | 8, |
| 234 | 9, |
| 235 | 10, |
| 236 | 12, |
| 237 | 13, |
| 238 | 14, |
| 239 | 16, |
| 240 | 17, |
| 241 | 18, |
| 242 | ] |
| 243 | "]] |
| 244 | .assert_debug_eq(&ids); |
| 245 | } |
| 246 | |
| 247 | // test for get_namespace_id |
| 248 | #[test] |
| 249 | fn test_get_namespace_id() { |
| 250 | let mut root = NamespaceTreeRoot::default(); |
| 251 | let mut names_to_query_buf = vec![]; |
| 252 | for i in 0..3 { |
| 253 | for j in 'a'..'d' { |
| 254 | let name = vec![Rc::from(format!("ns{i}")), Rc::from(format!("ns{j}"))]; |
| 255 | let _ = root.insert_or_find_namespace(name.clone()); |
| 256 | names_to_query_buf.push(name); |
| 257 | } |
| 258 | let name = vec![Rc::from(format!("ns{i}"))]; |
| 259 | let _ = root.insert_or_find_namespace(name.clone()); |
| 260 | names_to_query_buf.push(name); |
| 261 | } |
| 262 | let mut result_buf = vec![]; |
| 263 | for name in names_to_query_buf { |
| 264 | result_buf.push(root.get_namespace_id(name.iter().map(|x| &**x))); |
| 265 | } |
| 266 | expect![[r#" |
| 267 | [ |
| 268 | Some( |
| 269 | NamespaceId( |
| 270 | 8, |
| 271 | ), |
| 272 | ), |
| 273 | Some( |
| 274 | NamespaceId( |
| 275 | 9, |
| 276 | ), |
| 277 | ), |
| 278 | Some( |
| 279 | NamespaceId( |
| 280 | 10, |
| 281 | ), |
| 282 | ), |
| 283 | Some( |
| 284 | NamespaceId( |
| 285 | 7, |
| 286 | ), |
| 287 | ), |
| 288 | Some( |
| 289 | NamespaceId( |
| 290 | 12, |
| 291 | ), |
| 292 | ), |
| 293 | Some( |
| 294 | NamespaceId( |
| 295 | 13, |
| 296 | ), |
| 297 | ), |
| 298 | Some( |
| 299 | NamespaceId( |
| 300 | 14, |
| 301 | ), |
| 302 | ), |
| 303 | Some( |
| 304 | NamespaceId( |
| 305 | 11, |
| 306 | ), |
| 307 | ), |
| 308 | Some( |
| 309 | NamespaceId( |
| 310 | 16, |
| 311 | ), |
| 312 | ), |
| 313 | Some( |
| 314 | NamespaceId( |
| 315 | 17, |
| 316 | ), |
| 317 | ), |
| 318 | Some( |
| 319 | NamespaceId( |
| 320 | 18, |
| 321 | ), |
| 322 | ), |
| 323 | Some( |
| 324 | NamespaceId( |
| 325 | 15, |
| 326 | ), |
| 327 | ), |
| 328 | ] |
| 329 | "#]] |
| 330 | .assert_debug_eq(&result_buf); |
| 331 | } |
| 332 | |
| 333 | #[allow(clippy::too_many_lines)] |
| 334 | #[test] |
| 335 | fn test_tree_iter() { |
| 336 | let mut root = NamespaceTreeRoot::default(); |
| 337 | for i in 0..3 { |
| 338 | for j in 'a'..'d' { |
| 339 | let _ = root.insert_or_find_namespace( |
| 340 | vec![Rc::from(format!("ns{i}")), Rc::from(format!("ns{j}"))].into_iter(), |
| 341 | ); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | let result = root.iter().collect::<Vec<_>>(); |
| 346 | expect![[r#" |
| 347 | [ |
| 348 | [ |
| 349 | [], |
| 350 | ], |
| 351 | [ |
| 352 | [ |
| 353 | "Microsoft", |
| 354 | ], |
| 355 | ], |
| 356 | [ |
| 357 | [ |
| 358 | "Microsoft", |
| 359 | "Quantum", |
| 360 | ], |
| 361 | ], |
| 362 | [ |
| 363 | [ |
| 364 | "Microsoft", |
| 365 | "Quantum", |
| 366 | "Canon", |
| 367 | ], |
| 368 | [ |
| 369 | "Microsoft", |
| 370 | "Quantum", |
| 371 | "Canon", |
| 372 | ], |
| 373 | ], |
| 374 | [ |
| 375 | [ |
| 376 | "Microsoft", |
| 377 | "Quantum", |
| 378 | "Core", |
| 379 | ], |
| 380 | [ |
| 381 | "Microsoft", |
| 382 | "Quantum", |
| 383 | "Core", |
| 384 | ], |
| 385 | ], |
| 386 | [ |
| 387 | [ |
| 388 | "Microsoft", |
| 389 | "Quantum", |
| 390 | "Intrinsic", |
| 391 | ], |
| 392 | [ |
| 393 | "Microsoft", |
| 394 | "Quantum", |
| 395 | "Intrinsic", |
| 396 | ], |
| 397 | ], |
| 398 | [ |
| 399 | [ |
| 400 | "Microsoft", |
| 401 | "Quantum", |
| 402 | "Measurement", |
| 403 | ], |
| 404 | [ |
| 405 | "Microsoft", |
| 406 | "Quantum", |
| 407 | "Measurement", |
| 408 | ], |
| 409 | ], |
| 410 | [ |
| 411 | [ |
| 412 | "ns0", |
| 413 | ], |
| 414 | ], |
| 415 | [ |
| 416 | [ |
| 417 | "ns0", |
| 418 | "nsa", |
| 419 | ], |
| 420 | [ |
| 421 | "ns0", |
| 422 | "nsa", |
| 423 | ], |
| 424 | ], |
| 425 | [ |
| 426 | [ |
| 427 | "ns0", |
| 428 | "nsb", |
| 429 | ], |
| 430 | [ |
| 431 | "ns0", |
| 432 | "nsb", |
| 433 | ], |
| 434 | ], |
| 435 | [ |
| 436 | [ |
| 437 | "ns0", |
| 438 | "nsc", |
| 439 | ], |
| 440 | [ |
| 441 | "ns0", |
| 442 | "nsc", |
| 443 | ], |
| 444 | ], |
| 445 | [ |
| 446 | [ |
| 447 | "ns1", |
| 448 | ], |
| 449 | ], |
| 450 | [ |
| 451 | [ |
| 452 | "ns1", |
| 453 | "nsa", |
| 454 | ], |
| 455 | [ |
| 456 | "ns1", |
| 457 | "nsa", |
| 458 | ], |
| 459 | ], |
| 460 | [ |
| 461 | [ |
| 462 | "ns1", |
| 463 | "nsb", |
| 464 | ], |
| 465 | [ |
| 466 | "ns1", |
| 467 | "nsb", |
| 468 | ], |
| 469 | ], |
| 470 | [ |
| 471 | [ |
| 472 | "ns1", |
| 473 | "nsc", |
| 474 | ], |
| 475 | [ |
| 476 | "ns1", |
| 477 | "nsc", |
| 478 | ], |
| 479 | ], |
| 480 | [ |
| 481 | [ |
| 482 | "ns2", |
| 483 | ], |
| 484 | ], |
| 485 | [ |
| 486 | [ |
| 487 | "ns2", |
| 488 | "nsa", |
| 489 | ], |
| 490 | [ |
| 491 | "ns2", |
| 492 | "nsa", |
| 493 | ], |
| 494 | ], |
| 495 | [ |
| 496 | [ |
| 497 | "ns2", |
| 498 | "nsb", |
| 499 | ], |
| 500 | [ |
| 501 | "ns2", |
| 502 | "nsb", |
| 503 | ], |
| 504 | ], |
| 505 | [ |
| 506 | [ |
| 507 | "ns2", |
| 508 | "nsc", |
| 509 | ], |
| 510 | [ |
| 511 | "ns2", |
| 512 | "nsc", |
| 513 | ], |
| 514 | ], |
| 515 | ] |
| 516 | "#]] |
| 517 | .assert_debug_eq(&result); |
| 518 | } |
| 519 | |