From 42a87bdf97dd1672c485a09adc8cc3258ae61408 Mon Sep 17 00:00:00 2001 From: Ygg01 Date: Wed, 19 Feb 2025 09:08:55 +0100 Subject: [PATCH 1/2] Add test for atom order stability. --- string-cache-codegen/Cargo.toml | 4 +++ string-cache-codegen/lib.rs | 25 ++++++++++++++++--- .../tests/reproducibility_test.rs | 15 +++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 string-cache-codegen/tests/reproducibility_test.rs diff --git a/string-cache-codegen/Cargo.toml b/string-cache-codegen/Cargo.toml index 53c1f0f..bf17ce2 100644 --- a/string-cache-codegen/Cargo.toml +++ b/string-cache-codegen/Cargo.toml @@ -12,6 +12,10 @@ edition = "2018" name = "string_cache_codegen" path = "lib.rs" +[[test]] +name = "reproducibility_test" +harness = true + [dependencies] phf_generator = "0.11" phf_shared = "0.11" diff --git a/string-cache-codegen/lib.rs b/string-cache-codegen/lib.rs index 3228946..ad64db1 100644 --- a/string-cache-codegen/lib.rs +++ b/string-cache-codegen/lib.rs @@ -69,7 +69,7 @@ #![recursion_limit = "128"] use quote::quote; -use std::collections::HashSet; +use std::collections::BTreeSet; use std::fs::File; use std::io::{self, BufWriter, Write}; use std::path::Path; @@ -81,7 +81,7 @@ pub struct AtomType { static_set_doc: Option, macro_name: String, macro_doc: Option, - atoms: HashSet, + atoms: BTreeSet, } impl AtomType { @@ -114,7 +114,7 @@ impl AtomType { atom_doc: None, static_set_doc: None, macro_doc: None, - atoms: HashSet::new(), + atoms: BTreeSet::new(), } } @@ -181,6 +181,25 @@ impl AtomType { ) } + /// Write generated code to destination [`Vec`] and return it as [`String`] + /// + /// Used mostly for testing or displaying a value. + pub fn write_to_string(&mut self, mut destination: Vec) -> io::Result + { + destination.write_all( + self.to_tokens() + .to_string() + // Insert some newlines to make the generated code slightly easier to read. + .replace(" [ \"", "[\n\"") + .replace("\" , ", "\",\n") + .replace(" ( \"", "\n( \"") + .replace("; ", ";\n") + .as_bytes(), + )?; + let str = String::from_utf8(destination).unwrap(); + Ok(str) + } + fn to_tokens(&mut self) -> proc_macro2::TokenStream { // `impl Default for Atom` requires the empty string to be in the static set. // This also makes sure the set in non-empty, diff --git a/string-cache-codegen/tests/reproducibility_test.rs b/string-cache-codegen/tests/reproducibility_test.rs new file mode 100644 index 0000000..ff5f475 --- /dev/null +++ b/string-cache-codegen/tests/reproducibility_test.rs @@ -0,0 +1,15 @@ +use string_cache_codegen; + +#[test] +fn test_iteration_order() { + + let x1 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!") + .atoms(&["x", "xlink", "svg", "test"]) + .write_to_string(Vec::new()).unwrap(); + + let x2 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!") + .atoms(&["x", "xlink", "svg", "test"]) + .write_to_string(Vec::new()).unwrap(); + + assert_eq!(x1, x2); +} \ No newline at end of file From da84dd6a35f91fa646b9c40335044cdd9a5b6b0d Mon Sep 17 00:00:00 2001 From: Ygg01 Date: Wed, 19 Feb 2025 11:52:14 +0100 Subject: [PATCH 2/2] Made test for iteration order a unit test # Conflicts: # string-cache-codegen/tests/reproducibility_test.rs --- string-cache-codegen/Cargo.toml | 4 ---- string-cache-codegen/lib.rs | 14 ++++++++++++++ .../tests/reproducibility_test.rs | 15 --------------- 3 files changed, 14 insertions(+), 19 deletions(-) delete mode 100644 string-cache-codegen/tests/reproducibility_test.rs diff --git a/string-cache-codegen/Cargo.toml b/string-cache-codegen/Cargo.toml index bf17ce2..53c1f0f 100644 --- a/string-cache-codegen/Cargo.toml +++ b/string-cache-codegen/Cargo.toml @@ -12,10 +12,6 @@ edition = "2018" name = "string_cache_codegen" path = "lib.rs" -[[test]] -name = "reproducibility_test" -harness = true - [dependencies] phf_generator = "0.11" phf_shared = "0.11" diff --git a/string-cache-codegen/lib.rs b/string-cache-codegen/lib.rs index ad64db1..c703cf7 100644 --- a/string-cache-codegen/lib.rs +++ b/string-cache-codegen/lib.rs @@ -181,6 +181,7 @@ impl AtomType { ) } + #[cfg(test)] /// Write generated code to destination [`Vec`] and return it as [`String`] /// /// Used mostly for testing or displaying a value. @@ -334,3 +335,16 @@ impl AtomType { self.write_to(BufWriter::new(File::create(path)?)) } } + +#[test] +fn test_iteration_order() { + let x1 = crate::AtomType::new("foo::Atom", "foo_atom!") + .atoms(&["x", "xlink", "svg", "test"]) + .write_to_string(Vec::new()).expect("write to string cache x1"); + + let x2 = crate::AtomType::new("foo::Atom", "foo_atom!") + .atoms(&["x", "xlink", "svg", "test"]) + .write_to_string(Vec::new()).expect("write to string cache x2"); + + assert_eq!(x1, x2); +} \ No newline at end of file diff --git a/string-cache-codegen/tests/reproducibility_test.rs b/string-cache-codegen/tests/reproducibility_test.rs deleted file mode 100644 index ff5f475..0000000 --- a/string-cache-codegen/tests/reproducibility_test.rs +++ /dev/null @@ -1,15 +0,0 @@ -use string_cache_codegen; - -#[test] -fn test_iteration_order() { - - let x1 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!") - .atoms(&["x", "xlink", "svg", "test"]) - .write_to_string(Vec::new()).unwrap(); - - let x2 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!") - .atoms(&["x", "xlink", "svg", "test"]) - .write_to_string(Vec::new()).unwrap(); - - assert_eq!(x1, x2); -} \ No newline at end of file