Skip to content

Commit 1d88b5a

Browse files
committed
refactor(test): added test comments to assert statements
1 parent bfc2193 commit 1d88b5a

4 files changed

Lines changed: 1027 additions & 237 deletions

File tree

tests/test_database.rs

Lines changed: 125 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ fn test_insert_and_lookup_single_string() {
88
db.insert("hello".to_string());
99
let string_id = 0;
1010

11-
assert_eq!(db.get_string(string_id), Some("hello"));
11+
assert_eq!(
12+
db.get_string(string_id),
13+
Some("hello"),
14+
"Database should return 'hello' for string_id 0"
15+
);
1216

1317
let features_spurs = db.get_features(string_id).unwrap();
1418

@@ -23,17 +27,26 @@ fn test_insert_and_lookup_single_string() {
2327
.into_iter()
2428
.map(String::from)
2529
.collect();
26-
assert_eq!(features_strings, expected_features);
30+
assert_eq!(
31+
features_strings, expected_features,
32+
"Extracted features should match expected 2-grams with counts for 'hello'"
33+
);
2734

2835
let feature_size = features_spurs.len();
2936
for feature_spur in features_spurs {
3037
let ids = db.lookup_strings(feature_size, *feature_spur).unwrap();
31-
assert!(ids.contains(&string_id));
38+
assert!(
39+
ids.contains(&string_id),
40+
"Each feature should map back to string_id 0"
41+
);
3242
}
3343

3444
let non_existent_spur = interner.get("xx1");
3545
if let Some(spur) = non_existent_spur {
36-
assert!(db.lookup_strings(feature_size, spur).is_none());
46+
assert!(
47+
db.lookup_strings(feature_size, spur).is_none(),
48+
"Non-existent feature 'xx1' should not return any string IDs"
49+
);
3750
}
3851
}
3952

@@ -48,20 +61,34 @@ fn test_lookup_separates_by_size() {
4861
let features_1 = db.get_features(1).unwrap();
4962
let size_0 = features_0.len();
5063
let size_1 = features_1.len();
51-
assert_ne!(size_0, size_1);
64+
assert_ne!(
65+
size_0, size_1,
66+
"Feature count for 'hello' and 'hellos' should be different"
67+
);
5268

53-
// Fix: Bind the Arc to extend its lifetime
5469
let interner_arc = db.interner();
5570
let interner = interner_arc.lock().unwrap();
5671
let common_feature_spur = interner.get("#h1").unwrap();
5772

5873
let ids_for_size_0 = db.lookup_strings(size_0, common_feature_spur).unwrap();
59-
assert!(ids_for_size_0.contains(&0));
60-
assert!(!ids_for_size_0.contains(&1));
74+
assert!(
75+
ids_for_size_0.contains(&0),
76+
"Common feature '#h1' should be found in size bucket for 'hello' (id=0)"
77+
);
78+
assert!(
79+
!ids_for_size_0.contains(&1),
80+
"Size bucket for 'hello' should not contain 'hellos' (id=1)"
81+
);
6182

6283
let ids_for_size_1 = db.lookup_strings(size_1, common_feature_spur).unwrap();
63-
assert!(ids_for_size_1.contains(&1));
64-
assert!(!ids_for_size_1.contains(&0));
84+
assert!(
85+
ids_for_size_1.contains(&1),
86+
"Common feature '#h1' should be found in size bucket for 'hellos' (id=1)"
87+
);
88+
assert!(
89+
!ids_for_size_1.contains(&0),
90+
"Size bucket for 'hellos' should not contain 'hello' (id=0)"
91+
);
6592
}
6693

6794
#[test]
@@ -79,23 +106,51 @@ fn test_insert_multiple_strings_same_size() {
79106

80107
let at1_spur = interner.get("at1").unwrap();
81108
let ids = db.lookup_strings(feature_size, at1_spur).unwrap();
82-
assert!(ids.contains(&0));
83-
assert!(ids.contains(&1));
84-
assert!(!ids.contains(&2));
109+
assert!(
110+
ids.contains(&0),
111+
"Feature 'at1' should be found in 'cat' (id=0)"
112+
);
113+
assert!(
114+
ids.contains(&1),
115+
"Feature 'at1' should be found in 'bat' (id=1)"
116+
);
117+
assert!(
118+
!ids.contains(&2),
119+
"Feature 'at1' should not be found in 'dog' (id=2)"
120+
);
85121

86122
let og1_spur = interner.get("og1").unwrap();
87123
let ids_dog = db.lookup_strings(feature_size, og1_spur).unwrap();
88-
assert!(ids_dog.contains(&2));
89-
assert_eq!(ids_dog.len(), 1);
124+
assert!(
125+
ids_dog.contains(&2),
126+
"Feature 'og1' should be found in 'dog' (id=2)"
127+
);
128+
assert_eq!(
129+
ids_dog.len(),
130+
1,
131+
"Feature 'og1' should only be found in one string ('dog')"
132+
);
90133
}
91134

92135
#[test]
93136
fn test_initial_db_state() {
94137
let feature_extractor = Arc::new(CharacterNgrams::new(2, "$"));
95138
let db = HashDb::new(feature_extractor);
96-
assert_eq!(db.max_feature_len(), 0);
97-
assert_eq!(db.get_string(0), None);
98-
assert_eq!(db.get_features(0), None);
139+
assert_eq!(
140+
db.max_feature_len(),
141+
0,
142+
"Empty database should have max_feature_len of 0"
143+
);
144+
assert_eq!(
145+
db.get_string(0),
146+
None,
147+
"Empty database should return None for any string_id"
148+
);
149+
assert_eq!(
150+
db.get_features(0),
151+
None,
152+
"Empty database should return None for any feature lookup"
153+
);
99154
}
100155

101156
#[test]
@@ -107,9 +162,17 @@ fn test_string_collection_retrieval() {
107162
db.insert(s.to_string());
108163
}
109164
for (id, &expected_string) in corpus.iter().enumerate() {
110-
assert_eq!(db.get_string(id), Some(expected_string));
165+
assert_eq!(
166+
db.get_string(id),
167+
Some(expected_string),
168+
"Database should return correct string for id {id}"
169+
);
111170
}
112-
assert_eq!(db.get_string(corpus.len()), None);
171+
assert_eq!(
172+
db.get_string(corpus.len()),
173+
None,
174+
"Database should return None for out-of-bounds string_id"
175+
);
113176
}
114177

115178
#[test]
@@ -119,9 +182,18 @@ fn test_hashdb_debug_output() {
119182
db.insert("test".to_string());
120183
db.insert("apple".to_string());
121184
let debug_output = format!("{db:?}");
122-
assert!(debug_output.contains("num_strings: 2"));
123-
assert!(debug_output.contains("num_feature_size_buckets: 2"));
124-
assert!(debug_output.contains("total_unique_features_interned: 11"));
185+
assert!(
186+
debug_output.contains("num_strings: 2"),
187+
"Debug output should show 2 strings inserted"
188+
);
189+
assert!(
190+
debug_output.contains("num_feature_size_buckets: 2"),
191+
"Debug output should show 2 feature size buckets (different lengths)"
192+
);
193+
assert!(
194+
debug_output.contains("total_unique_features_interned: 11"),
195+
"Debug output should show 11 total unique features interned"
196+
);
125197
}
126198

127199
#[test]
@@ -130,16 +202,34 @@ fn test_db_clear() {
130202
let mut db = HashDb::new(feature_extractor);
131203
db.insert("test".to_string());
132204

133-
// Ensure DB is not empty
134-
assert_eq!(db.get_string(0), Some("test"));
135-
assert_eq!(db.max_feature_len(), 5);
205+
assert_eq!(
206+
db.get_string(0),
207+
Some("test"),
208+
"Database should contain 'test' before clear"
209+
);
210+
assert_eq!(
211+
db.max_feature_len(),
212+
5,
213+
"Database should have max_feature_len of 5 before clear"
214+
);
136215

137216
db.clear();
138217

139-
// Ensure DB is now empty
140-
assert_eq!(db.get_string(0), None);
141-
assert_eq!(db.max_feature_len(), 0);
142-
assert_eq!(db.interner().lock().unwrap().len(), 0);
218+
assert_eq!(
219+
db.get_string(0),
220+
None,
221+
"Database should return None after clear"
222+
);
223+
assert_eq!(
224+
db.max_feature_len(),
225+
0,
226+
"Database should have max_feature_len of 0 after clear"
227+
);
228+
assert_eq!(
229+
db.interner().lock().unwrap().len(),
230+
0,
231+
"Interner should be empty after clear"
232+
);
143233
}
144234

145235
#[test]
@@ -149,5 +239,9 @@ fn test_total_strings() {
149239
db.insert("test1".to_string());
150240
db.insert("test2".to_string());
151241

152-
assert_eq!(db.total_strings(), 2);
242+
assert_eq!(
243+
db.total_strings(),
244+
2,
245+
"Database should report 2 total strings after inserting 2 strings"
246+
);
153247
}

0 commit comments

Comments
 (0)