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