|
| 1 | +use crate::{ |
| 2 | + compressor::{Compressor, Level, Stats}, |
| 3 | + StateGroupEntry, |
| 4 | +}; |
| 5 | +use state_map::StateMap; |
| 6 | +use std::collections::BTreeMap; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn stats_correct_when_no_resets() { |
| 10 | + let mut initial: BTreeMap<i64, StateGroupEntry> = BTreeMap::new(); |
| 11 | + let mut prev = None; |
| 12 | + |
| 13 | + // This starts with the following structure |
| 14 | + // |
| 15 | + // 0-1-2-3-4-5-6-7-8-9-10-11-12-13 |
| 16 | + for i in 0i64..=13i64 { |
| 17 | + initial.insert( |
| 18 | + i, |
| 19 | + StateGroupEntry { |
| 20 | + in_range: true, |
| 21 | + prev_state_group: prev, |
| 22 | + state_map: StateMap::new(), |
| 23 | + }, |
| 24 | + ); |
| 25 | + |
| 26 | + prev = Some(i) |
| 27 | + } |
| 28 | + |
| 29 | + let mut compressor = Compressor { |
| 30 | + original_state_map: &initial, |
| 31 | + new_state_group_map: BTreeMap::new(), |
| 32 | + levels: vec![Level::new(3), Level::new(3)], |
| 33 | + stats: Stats::default(), |
| 34 | + }; |
| 35 | + |
| 36 | + // This should create the following structure |
| 37 | + // |
| 38 | + // 0 3\ 12 |
| 39 | + // 1 4 6\ 13 |
| 40 | + // 2 5 7 9 |
| 41 | + // 8 10 |
| 42 | + // 11 |
| 43 | + compressor.create_new_tree(); |
| 44 | + |
| 45 | + // No resets should have taken place |
| 46 | + assert_eq!(compressor.stats.resets_no_suitable_prev, 0); |
| 47 | + assert_eq!(compressor.stats.resets_no_suitable_prev_size, 0); |
| 48 | + |
| 49 | + // Groups 3,6,9,12 should be the only ones changed |
| 50 | + assert_eq!(compressor.stats.state_groups_changed, 4); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn stats_correct_when_some_resets() { |
| 55 | + let mut initial: BTreeMap<i64, StateGroupEntry> = BTreeMap::new(); |
| 56 | + let mut prev = None; |
| 57 | + |
| 58 | + // This starts with the following structure |
| 59 | + // |
| 60 | + // (note missing 3-4 link) |
| 61 | + // 0-1-2-3 |
| 62 | + // 4-5-6-7-8-9-10-11-12-13 |
| 63 | + // |
| 64 | + // Each group i has state: |
| 65 | + // ('node','is', i) |
| 66 | + // ('group', j, 'seen') where j is ancestor of i |
| 67 | + for i in 0i64..=13i64 { |
| 68 | + if i == 4 { |
| 69 | + prev = None |
| 70 | + } |
| 71 | + let mut entry = StateGroupEntry { |
| 72 | + in_range: true, |
| 73 | + prev_state_group: prev, |
| 74 | + state_map: StateMap::new(), |
| 75 | + }; |
| 76 | + entry |
| 77 | + .state_map |
| 78 | + .insert("group", &i.to_string(), "seen".into()); |
| 79 | + entry.state_map.insert("node", "is", i.to_string().into()); |
| 80 | + |
| 81 | + initial.insert(i, entry); |
| 82 | + |
| 83 | + prev = Some(i) |
| 84 | + } |
| 85 | + |
| 86 | + let mut compressor = Compressor { |
| 87 | + original_state_map: &initial, |
| 88 | + new_state_group_map: BTreeMap::new(), |
| 89 | + levels: vec![Level::new(3), Level::new(3)], |
| 90 | + stats: Stats::default(), |
| 91 | + }; |
| 92 | + |
| 93 | + // This should create the following structure |
| 94 | + // |
| 95 | + // Brackets mean that has NO predecessor but is in that position in the |
| 96 | + // levels tree |
| 97 | + // |
| 98 | + // 0 3\ 12 |
| 99 | + // 1 (4)(6)\ 13 |
| 100 | + // 2 5 7 9 |
| 101 | + // 8 10 |
| 102 | + // 11 |
| 103 | + compressor.create_new_tree(); |
| 104 | + |
| 105 | + // the reset required for 4 contributes 2 to the size stat |
| 106 | + // - (1 'node' and 1 'group') entry |
| 107 | + // the reset required for 6 contributes 4 to the size stat |
| 108 | + // - (1 'node' and 3 'group') entry |
| 109 | + assert_eq!(compressor.stats.resets_no_suitable_prev, 2); |
| 110 | + assert_eq!(compressor.stats.resets_no_suitable_prev_size, 6); |
| 111 | + |
| 112 | + // groups 3,4,6,9,12 are the only ones changed |
| 113 | + assert_eq!(compressor.stats.state_groups_changed, 5); |
| 114 | +} |
| 115 | + |
| 116 | +#[test] |
| 117 | +fn stats_correct_if_no_changes() { |
| 118 | + // This should create the following structure |
| 119 | + // |
| 120 | + // 0 3\ 12 |
| 121 | + // 1 4 6\ 13 |
| 122 | + // 2 5 7 9 |
| 123 | + // 8 10 |
| 124 | + // 11 |
| 125 | + let initial_edges: BTreeMap<i64, i64> = vec![ |
| 126 | + (1, 0), |
| 127 | + (2, 1), |
| 128 | + (4, 3), |
| 129 | + (5, 4), |
| 130 | + (6, 3), |
| 131 | + (7, 6), |
| 132 | + (8, 7), |
| 133 | + (9, 6), |
| 134 | + (10, 9), |
| 135 | + (11, 10), |
| 136 | + (13, 12), |
| 137 | + ] |
| 138 | + .into_iter() |
| 139 | + .collect(); |
| 140 | + |
| 141 | + let mut initial: BTreeMap<i64, StateGroupEntry> = BTreeMap::new(); |
| 142 | + |
| 143 | + for i in 0i64..=13i64 { |
| 144 | + // edge from map |
| 145 | + let pred_group = initial_edges.get(&i); |
| 146 | + |
| 147 | + // Need Option<i64> not Option<&i64> |
| 148 | + let prev; |
| 149 | + match pred_group { |
| 150 | + Some(i) => prev = Some(*i), |
| 151 | + None => prev = None, |
| 152 | + } |
| 153 | + |
| 154 | + // insert that edge into the initial map |
| 155 | + initial.insert( |
| 156 | + i, |
| 157 | + StateGroupEntry { |
| 158 | + in_range: true, |
| 159 | + prev_state_group: prev, |
| 160 | + state_map: StateMap::new(), |
| 161 | + }, |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + let mut compressor = Compressor { |
| 166 | + original_state_map: &initial, |
| 167 | + new_state_group_map: BTreeMap::new(), |
| 168 | + levels: vec![Level::new(3), Level::new(3)], |
| 169 | + stats: Stats::default(), |
| 170 | + }; |
| 171 | + |
| 172 | + // This should create the following structure (i.e. no change) |
| 173 | + // |
| 174 | + // 0 3\ 12 |
| 175 | + // 1 4 6\ 13 |
| 176 | + // 2 5 7 9 |
| 177 | + // 8 10 |
| 178 | + // 11 |
| 179 | + compressor.create_new_tree(); |
| 180 | + |
| 181 | + // No changes should have been made (the old tree should be the same) |
| 182 | + assert_eq!(compressor.stats.resets_no_suitable_prev, 0); |
| 183 | + assert_eq!(compressor.stats.resets_no_suitable_prev_size, 0); |
| 184 | + assert_eq!(compressor.stats.state_groups_changed, 0); |
| 185 | +} |
0 commit comments