Skip to content

Commit

Permalink
improved testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDefuria committed Nov 5, 2023
1 parent 51c3fd8 commit 826f9b8
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 96 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ crate-type = ["cdylib", "rlib"]
path = "src/lib.rs"
name = "circuit_solver_algorithms"




[dependencies]
serde = { version = "1.0.177", features = ["derive", "rc"] }
serde_json = "1.0.96"
Expand Down
4 changes: 2 additions & 2 deletions src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Container {
cleaned
}

pub fn create_super_nodes(&mut self) -> &mut Self {
pub fn create_super_nodes(&mut self) -> Result<&mut Self, String> {
let mut super_nodes: Vec<Tool> = Vec::new();
let mut valid_sources: Vec<Weak<RefCell<Element>>> = Vec::new();
for element in &self.elements {
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Container {
self.add_tool(node);
}

self
Ok(self)
}

pub fn create_meshes(&mut self) -> &mut Self {
Expand Down
32 changes: 19 additions & 13 deletions src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::cell::RefCell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsValue;
use crate::validation::StatusError::Known;

#[derive(Serialize, Deserialize)]
pub struct ContainerSetup {
Expand All @@ -36,7 +37,7 @@ pub fn get_tools(container_js: JsValue) -> Result<String, StatusError> {
let mut c: Container = Container::from(setup);
c.validate()?;
c.create_nodes()?;
c.create_super_nodes();
c.create_super_nodes()?;
let nodes: Vec<Vec<usize>> = c
.nodes()
.iter()
Expand All @@ -47,15 +48,20 @@ pub fn get_tools(container_js: JsValue) -> Result<String, StatusError> {
}

#[wasm_bindgen]
pub fn solve(matrix: bool, nodal: bool, container_js: JsValue) -> Result<String, JsValue> {
let setup: ContainerSetup = from_value(container_js)?;
pub fn solve(matrix: bool, nodal: bool, container_js: JsValue) -> Result<String, String> {
let setup: ContainerSetup = if let Ok(setup) = from_value(container_js) {
setup
} else {
return Err(String::from(Known("Failed to parse and deserialize input case".to_string())));
};

let mut c: Container = Container::from(setup);
c.validate()?;

return match nodal {
true => {
c.create_nodes()?;
c.create_super_nodes();
c.create_super_nodes()?;
let steps: Vec<Step>;
if matrix {
let mut solver: NodeMatrixSolver = Solver::new(Rc::new(RefCell::new(c)));
Expand All @@ -69,28 +75,28 @@ pub fn solve(matrix: bool, nodal: bool, container_js: JsValue) -> Result<String,
false => {
c.create_meshes();
c.create_super_meshes();
Err(JsValue::from(format!(
Err(format!(
"{} Solver not implemented for meshes",
if matrix { "Matrix" } else { "Step" }
)))
))
}
};
}

#[wasm_bindgen]
pub fn return_solved_step_example() -> Result<String, JsValue> {
pub fn return_solved_step_example() -> Result<String, String> {
let mut c: Container = create_mna_container();
c.create_nodes()?;
c.create_super_nodes();
c.create_super_nodes()?;
let mut solver: NodeStepSolver = Solver::new(Rc::new(RefCell::new(c)));
serialize_steps(solver.solve()?)
}

#[wasm_bindgen]
pub fn return_solved_matrix_example() -> Result<String, JsValue> {
pub fn return_solved_matrix_example() -> Result<String, String> {
let mut c: Container = create_mna_container();
c.create_nodes()?;
c.create_super_nodes();
c.create_super_nodes()?;
let mut solver: NodeMatrixSolver = Solver::new(Rc::new(RefCell::new(c)));
serialize_steps(solver.solve()?)
}
Expand All @@ -101,12 +107,12 @@ pub fn test_wasm() -> String {
}

#[wasm_bindgen]
pub fn test_error() -> Result<String, JsValue> {
Err(JsValue::from_str("Error from Rust! 🦀🦀🦀"))
pub fn test_error() -> Result<String, String> {
Err("Error from Rust! 🦀🦀🦀".to_string())
}

#[wasm_bindgen]
pub fn solve_test_container(container_id: i32) -> Result<String, JsValue> {
pub fn solve_test_container(container_id: i32) -> Result<String, String> {
let c: Container = match container_id {
0 => create_basic_container(),
1 => create_basic_supernode_container(),
Expand Down
4 changes: 2 additions & 2 deletions src/solvers/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ impl From<Step> for JsValue {
}
}

pub fn serialize_steps(steps: Vec<Step>) -> Result<String, JsValue> {
pub fn serialize_steps(steps: Vec<Step>) -> Result<String, String> {
match serde_json::to_string(&steps) {
Ok(a) => Ok(a),
Err(_) => Err(JsValue::from("Error serializing steps")),
Err(_) => Err("Error serializing steps".to_string()),
}
}

Expand Down
25 changes: 24 additions & 1 deletion src/validation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::RefCell;
use std::fmt::{Debug, Display, Formatter};
use std::rc::{Rc, Weak};
use serde::{Deserialize, Serialize};

use wasm_bindgen::JsValue;

Expand All @@ -18,7 +19,7 @@ pub enum Status {
/// Possible Issues
///
/// Valid: Container is valid
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum StatusError {
Unknown,
Known(String),
Expand Down Expand Up @@ -67,6 +68,28 @@ impl From<String> for StatusError {
}
}

impl From<JsValue> for StatusError {
fn from(value: JsValue) -> Self {
StatusError::Known(value.as_string().unwrap_or("Unknown".to_string()))
}
}

impl From<StatusError> for String {
fn from(error: StatusError) -> Self {
let contents = match error {
StatusError::Unknown => "Unknown Issue".to_string(),
StatusError::Known(str) => format!("\"Known Issue... {}\"", str),
StatusError::Multiple(error_list) => error_list
.iter()
.map(|x| format!("\"{}\"", x))
.collect::<Vec<String>>()
.join(", ")
};

format!("{{\"errors\": [{contents}]}}")
}
}

impl From<StatusError> for JsValue {
fn from(error: StatusError) -> Self {
JsValue::from_str(&format!("{}", error))
Expand Down
2 changes: 0 additions & 2 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ regex-lite = "0.1.0"
circuit-solver-algorithms = { path = "../" }
wasm-bindgen-test = "0.3.0"
diff = "0.1.12"

[dev-dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"elements": [
{
"np": [
"3"
],
"nn": [
"R1"
],
"value": 10,
"id": 0,
"positive": [
1
],
"negative": [
2
],
"class": "Resistor"
},
{
"np": [
"R0"
],
"nn": [
"3",
"4"
],
"value": 10,
"id": 1,
"positive": [
2,
3
],
"negative": [
0
],
"class": "Resistor"
},
{
"np": [
"R0"
],
"nn": [
"R1",
"4"
],
"value": 10,
"id": 2,
"positive": [
1,
3
],
"negative": [
0
]
},
{
"np": [
"R1",
"3"
],
"nn": [],
"value": 10,
"id": 3,
"positive": [],
"negative": [
1,
2
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errors": [
"Known Issue... Failed to parse and deserialize input case"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
],
"npIndex": {
"x": 6,
"y": 9
"y": 7
},
"nnIndex": {
"x": 19,
"y": 8
"x": 17,
"y": 9
},
"value": 10,
"id": 0,
Expand All @@ -37,12 +37,12 @@
"R2"
],
"npIndex": {
"x": 19,
"y": 8
"x": 17,
"y": 9
},
"nnIndex": {
"x": 11,
"y": 17
"x": 12,
"y": 18
},
"value": 10,
"id": 1,
Expand All @@ -58,26 +58,26 @@
"type": 3,
"designator": "R2",
"np": [
"R0"
"R1"
],
"nn": [
"R1"
"R0"
],
"npIndex": {
"x": 6,
"y": 9
"x": 12,
"y": 18
},
"nnIndex": {
"x": 11,
"y": 17
"x": 6,
"y": 7
},
"value": 10,
"id": 2,
"positive": [
1
0
],
"negative": [
0
1
],
"class": "Resistor"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Multiple": [
{
"Known": "Element cannot have id 0"
},
{
"Known": "No Sources"
},
{
"Known": "Multiple Grounds"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Empty file.
Loading

0 comments on commit 826f9b8

Please sign in to comment.