Skip to content

Commit bbcb18e

Browse files
committed
Tests for load/store
1 parent 70ae180 commit bbcb18e

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

algo/src/rank/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
55
*/
66

7+
//! Ranking algorithms for graphs.
8+
79
pub mod pagerank;
810
pub use pagerank::{Mode, PageRank};

cli/src/lib.rs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub struct MemoryUsageArg {
195195
}
196196

197197
#[derive(Debug, Clone, Copy, ValueEnum)]
198-
/// How to store vectors of floats.
198+
/// Formats for storing and loading vectors of floats.
199199
pub enum FloatVectorFormat {
200200
/// Java-compatible format: a sequence of big-endian floats (32 or 64 bits).
201201
Java,
@@ -227,8 +227,9 @@ impl FloatVectorFormat {
227227
{
228228
create_parent_dir(&path)?;
229229
let path_display = path.as_ref().display();
230-
let mut file = std::fs::File::create(&path)
230+
let file = std::fs::File::create(&path)
231231
.with_context(|| format!("Could not create vector at {}", path_display))?;
232+
let mut file = BufWriter::new(file);
232233

233234
match self {
234235
FloatVectorFormat::Epserde => {
@@ -1086,6 +1087,88 @@ mod tests {
10861087
.unwrap();
10871088
assert!(path.exists());
10881089
}
1090+
1091+
#[test]
1092+
fn test_roundtrip_ascii_f64() {
1093+
let dir = tempfile::tempdir().unwrap();
1094+
let path = dir.path().join("test.txt");
1095+
let values: Vec<f64> = vec![1.5, 2.75, 3.0, 0.0, -1.25];
1096+
FloatVectorFormat::Ascii
1097+
.store(&path, &values, None)
1098+
.unwrap();
1099+
let loaded: Vec<f64> = FloatVectorFormat::Ascii.load(&path).unwrap();
1100+
assert_eq!(loaded, values);
1101+
}
1102+
1103+
#[test]
1104+
fn test_roundtrip_json_f64() {
1105+
let dir = tempfile::tempdir().unwrap();
1106+
let path = dir.path().join("test.json");
1107+
let values: Vec<f64> = vec![1.5, 2.75, 3.0, 0.0, -1.25];
1108+
FloatVectorFormat::Json
1109+
.store(&path, &values, None)
1110+
.unwrap();
1111+
let loaded: Vec<f64> = FloatVectorFormat::Json.load(&path).unwrap();
1112+
assert_eq!(loaded, values);
1113+
}
1114+
1115+
#[test]
1116+
fn test_roundtrip_java_f64() {
1117+
let dir = tempfile::tempdir().unwrap();
1118+
let path = dir.path().join("test.bin");
1119+
let values: Vec<f64> = vec![1.5, 2.75, 3.0, 0.0, -1.25];
1120+
FloatVectorFormat::Java
1121+
.store(&path, &values, None)
1122+
.unwrap();
1123+
let loaded: Vec<f64> = FloatVectorFormat::Java.load(&path).unwrap();
1124+
assert_eq!(loaded, values);
1125+
}
1126+
1127+
#[test]
1128+
fn test_roundtrip_epserde_f64() {
1129+
let dir = tempfile::tempdir().unwrap();
1130+
let path = dir.path().join("test.bin");
1131+
let values: Vec<f64> = vec![1.5, 2.75, 3.0, 0.0, -1.25];
1132+
FloatVectorFormat::Epserde
1133+
.store(&path, &values, None)
1134+
.unwrap();
1135+
let loaded: Vec<f64> = FloatVectorFormat::Epserde.load(&path).unwrap();
1136+
assert_eq!(loaded, values);
1137+
}
1138+
1139+
#[test]
1140+
fn test_roundtrip_empty() {
1141+
let dir = tempfile::tempdir().unwrap();
1142+
for (fmt, ext) in [
1143+
(FloatVectorFormat::Ascii, "txt"),
1144+
(FloatVectorFormat::Json, "json"),
1145+
(FloatVectorFormat::Java, "bin"),
1146+
(FloatVectorFormat::Epserde, "eps"),
1147+
] {
1148+
let path = dir.path().join(format!("empty.{ext}"));
1149+
let values: Vec<f64> = vec![];
1150+
fmt.store(&path, &values, None).unwrap();
1151+
let loaded: Vec<f64> = fmt.load(&path).unwrap();
1152+
assert_eq!(loaded, values, "roundtrip failed for {ext}");
1153+
}
1154+
}
1155+
1156+
#[test]
1157+
fn test_roundtrip_f32() {
1158+
let dir = tempfile::tempdir().unwrap();
1159+
let values: Vec<f32> = vec![1.5, 2.75, 3.0, 0.0, -1.25];
1160+
for (fmt, ext) in [
1161+
(FloatVectorFormat::Ascii, "txt"),
1162+
(FloatVectorFormat::Json, "json"),
1163+
(FloatVectorFormat::Java, "bin"),
1164+
(FloatVectorFormat::Epserde, "eps"),
1165+
] {
1166+
let path = dir.path().join(format!("f32.{ext}"));
1167+
fmt.store(&path, &values, None).unwrap();
1168+
let loaded: Vec<f32> = fmt.load(&path).unwrap();
1169+
assert_eq!(loaded, values, "f32 roundtrip failed for {ext}");
1170+
}
1171+
}
10891172
}
10901173

10911174
mod int_vector_format {

0 commit comments

Comments
 (0)