| 1000 | |
| 1001 | #[test] |
| 1002 | fn test_topological_sort() { |
| 1003 | let var0 = Expr::var(0); |
| 1004 | let var1 = Expr::var(1); |
| 1005 | let app = Expr::app(var0, var1); |
| 1006 | |
| 1007 | let (info_map, _, topo) = analyze_block(&[app], false); |
| 1008 | |
| 1009 | // Should have all hashes |
| 1010 | assert_eq!(topo.len(), info_map.len()); |
| 1011 | |
| 1012 | // Leaves (Var) should come before App |
| 1013 | let app_hash = info_map |
| 1014 | .iter() |
| 1015 | .find(|(_, info)| matches!(info.expr.as_ref(), Expr::App(..))) |
| 1016 | .map(|(h, _)| *h); |
| 1017 | |
| 1018 | if let Some(app_h) = app_hash { |
| 1019 | let app_pos = topo.iter().position(|h| *h == app_h).unwrap(); |
| 1020 | // App should be last (after its children) |
| 1021 | for child_hash in &info_map.get(&app_h).unwrap().children { |
| 1022 | let child_pos = topo.iter().position(|h| h == child_hash).unwrap(); |
| 1023 | assert!( |
| 1024 | child_pos < app_pos, |
| 1025 | "Child should come before parent in topo order" |
| 1026 | ); |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | #[test] |
| 1032 | fn test_build_sharing_vec() { |