Skip to content

Commit

Permalink
Fix up Rust benchmarks and add tests.
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
lgarron committed Aug 24, 2023
1 parent ace8134 commit c01a33d
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 40 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 51 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,71 @@ build: build/bin/twsearch
all: build/bin/twsearch build/esm build-rust

.PHONY: test
test: test-warning lint test-cpp-cli test-twsearch-cpp-wrapper-cli test-cpp_port-all
test: \
test-warning \
lint \
test-cpp-cli \
test-twsearch-cpp-wrapper-cli \
test-rs-all \
benchmark-rs-all

.PHONY: test-warning
test-warning:
@echo "Warning: tests are slow to run right now."

# C++ and `twsearch-cpp-wrapper` testing

.PHONY: test-cpp-cli
test-cpp-cli: build/bin/twsearch
cargo run --package twsearch-cpp-wrapper --example test-cpp-cli
cargo run --package twsearch-cpp-wrapper \
--example test-cpp-cli

.PHONY: test-twsearch-cpp-wrapper-cli
test-twsearch-cpp-wrapper-cli:
cargo run --package twsearch-cpp-wrapper --example test-twsearch-cpp-wrapper-cli
cargo run --package twsearch-cpp-wrapper \
--example test-twsearch-cpp-wrapper-cli

# Rust testing

.PHONY: test-rs-all
test-rs-all: \
test-rs-default \
test-rs-orientation_packer \
test-rs-no_orientation_mod

TEST_RS = cargo test --package twsearch

.PHONY: test-rs-default
test-rs-default:
${TEST_RS}

.PHONY: test-cpp_port-all
test-cpp_port-all: test-cpp_port-default test-cpp_port-orientation_packer test-cpp_port-no_orientation_mod
.PHONY: test-rs-orientation_packer
test-rs-orientation_packer:
${TEST_RS} --features orientation_packer

.PHONY: test-cpp_port-default
test-cpp_port-default:
cargo run --release --example test-cpp_port
.PHONY: test-rs-no_orientation_mod
test-rs-no_orientation_mod:
${TEST_RS} --features no_orientation_mod

.PHONY: test-cpp_port-orientation_packer
test-cpp_port-orientation_packer:
cargo run --features orientation_packer --release --example test-cpp_port
BENCHMARK_RS = cargo run --package twsearch --release --example benchmark

.PHONY: test-cpp_port-no_orientation_mod
test-cpp_port-no_orientation_mod:
cargo run --features no_orientation_mod --release --example test-cpp_port
.PHONY: benchmark-rs-all
benchmark-rs-all: \
benchmark-rs-default \
benchmark-rs-orientation_packer \
benchmark-rs-no_orientation_mod

.PHONY: benchmark-rs-default
benchmark-rs-default:
${BENCHMARK_RS}

.PHONY: benchmark-rs-orientation_packer
benchmark-rs-orientation_packer:
${BENCHMARK_RS} --features orientation_packer

.PHONY: benchmark-rs-no_orientation_mod
benchmark-rs-no_orientation_mod:
${BENCHMARK_RS} --features no_orientation_mod

.PHONY: clean
clean:
Expand All @@ -42,7 +79,6 @@ clean:
cpp-clean:
rm -rf ./build


.PHONY: reset
reset: clean
rm -rf ./emsdk ./node_modules
Expand Down
2 changes: 0 additions & 2 deletions src/cpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ version = "0.0.0"
edition = "2021"

[dependencies]
cityhash = "0.1.1"
clap = { version = "4.2.7", features = ["derive"] }
clap_complete = "4.2.1"
cubing = "0.5.4"
cxx = "1.0"
derive_more = "0.99.17"
lazy_static = "1.4.0"
num_cpus = "1.15.0"
regex = "1.8.1"
Expand Down
7 changes: 0 additions & 7 deletions src/rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ no_orientation_mod = []

[dependencies]
cityhash = "0.1.1"
clap = { version = "4.2.7", features = ["derive"] }
clap_complete = "4.2.1"
cubing = "0.5.4"
derive_more = "0.99.17"
lazy_static = "1.4.0"
regex = "1.8.1"
serde = { version = "1.0.160", features = ["derive", "rc"] }
serde_json = "1.0.96"
tempfile = "3.5.0"

[dev-dependencies]
wait-timeout = "0.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use std::collections::HashMap;
#[cfg(not(feature = "no_orientation_mod"))]
use std::sync::Arc;

use crate::cpp_port::PackedKPuzzle;

#[path = "./cpp_port/mod.rs"]
mod cpp_port;
use twsearch::PackedKPuzzle;

const PRINT_FINAL_STATE: bool = false;

Expand Down
96 changes: 96 additions & 0 deletions src/rs/packed/orientation_packer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,99 @@ impl OrientationPacker {
+ usize_to_u8(orientation_with_mod.orientation)
}
}

#[cfg(test)]
mod tests {
use cubing::kpuzzle::{
KPuzzle, KPuzzleDefinition, KPuzzleOrbitDefinition, KPuzzleOrbitName, KState, KStateData,
KStateOrbitData, KTransformationData, KTransformationOrbitData,
};
use std::collections::HashMap;
use std::sync::Arc;

use crate::PackedKPuzzle;

#[test]
fn test_orientation_mod() {
let def = KPuzzleDefinition {
name: "custom".to_owned(),
orbit_ordering: Some(vec![KPuzzleOrbitName("PIECES".to_owned())]),
orbits: HashMap::from([(
KPuzzleOrbitName("PIECES".to_owned()),
KPuzzleOrbitDefinition {
num_pieces: 2,
num_orientations: 12,
},
)]),
start_state_data: KStateData::from([(
KPuzzleOrbitName("PIECES".to_owned()),
KStateOrbitData {
pieces: vec![0, 1],
orientation: vec![0, 0],
orientation_mod: Some(vec![3, 4]),
},
)])
.into(),
moves: HashMap::from([
(
"SPIN".try_into().unwrap(),
Arc::new(KTransformationData::from([(
KPuzzleOrbitName("PIECES".to_owned()),
KTransformationOrbitData {
permutation: vec![0, 1], // TODO: is this actually L'?
orientation: vec![2, 5],
},
)])),
),
(
"SWAP".try_into().unwrap(),
Arc::new(KTransformationData::from([(
KPuzzleOrbitName("PIECES".to_owned()),
KTransformationOrbitData {
permutation: vec![1, 0], // TODO: is this actually R'?
orientation: vec![0, 0],
},
)])),
),
]),
experimental_derived_moves: None,
};
let kpuzzle = KPuzzle::try_new(def).unwrap();
let packed_kpuzzle = PackedKPuzzle::try_from(kpuzzle.clone()).unwrap();

let spin = packed_kpuzzle
.transformation_from_move(&"SPIN".try_into().unwrap())
.unwrap();
let swap = packed_kpuzzle
.transformation_from_move(&"SWAP".try_into().unwrap())
.unwrap();

let state = packed_kpuzzle.start_state();
// println!("{:?}", state.unpack().state_data);

let state = state.apply_transformation(&spin);
// println!("{:?}", state.unpack().state_data);

let state = state.apply_transformation(&swap);
// println!("{:?}", state.unpack().state_data);

let state = state.apply_transformation(&spin);
// println!("{:?}", state.unpack().state_data);

let expected = KState {
kpuzzle,
state_data: KStateData::from([(
KPuzzleOrbitName("PIECES".to_owned()),
KStateOrbitData {
pieces: vec![1, 0],
orientation: vec![3, 1],
orientation_mod: Some(vec![4, 3]),
},
)])
.into(),
};
// println!("{:?}", expected.state_data);
assert_eq!(state.unpack(), expected);
println!("Custom puzzle test passes!\n--------");
}
}
6 changes: 3 additions & 3 deletions src/rs/packed/packed_kpuzzle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::byte_conversions::u8_to_usize;
use super::orientation_packer::OrientationPacker;
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
use crate::cpp_port::packed::orientation_packer::OrientationWithMod;
use crate::packed::orientation_packer::OrientationWithMod;

// https://github.com/cubing/twsearch/issues/25#issue-1862613355
#[cfg(not(feature = "orientation_packer"))]
Expand Down Expand Up @@ -148,7 +148,7 @@ impl PackedKPuzzle {
#[cfg(not(feature = "orientation_packer"))]
#[cfg(not(feature = "no_orientation_mod"))]
{
if std::convert::Into::<usize>::into(orbit_info.num_orientations) % orientation_mod[i] != 0 {
if orientation_mod[i] != 0 && std::convert::Into::<usize>::into(orbit_info.num_orientations) % orientation_mod[i] != 0 {
eprintln!(
"`orientation_mod` of {} seen for piece at index {} in orbit {} in the start state for puzzle {}. This must be a factor of `num_orientations` for the orbit ({}). See: https://js.cubing.net/cubing/api/interfaces/kpuzzle.KStateOrbitData.html#orientationMod",
orientation_mod[i],
Expand All @@ -165,7 +165,7 @@ impl PackedKPuzzle {
#[cfg(feature = "orientation_packer")]
#[cfg(not(feature = "no_orientation_mod"))]
{
if u8_to_usize(orbit_info.num_orientations) % orientation_mod[i] != 0 {
if orientation_mod[i] != 0 && u8_to_usize(orbit_info.num_orientations) % orientation_mod[i] != 0 {
eprintln!(
"`orientation_mod` of {} seen for piece at index {} in orbit {} in the start state for puzzle {}. This must be a factor of `num_orientations` for the orbit ({}). See: https://js.cubing.net/cubing/api/interfaces/kpuzzle.KStateOrbitData.html#orientationMod",
orientation_mod[i],
Expand Down

0 comments on commit c01a33d

Please sign in to comment.