Option to hide false or true values in truthTable

This commit is contained in:
Martin Berg Alstad 2024-06-16 00:36:59 +02:00
parent b9998ce7bf
commit 9431eb2fa9
2 changed files with 73 additions and 6 deletions

View File

@ -82,3 +82,21 @@ GET {{url}}/simplify/table/{{expression}}?sort=TRUE_FIRST
}
});
%}
### GET with table and hide false values
< {%
import {expression} from "./common";
expression("A & B | C")
%}
GET {{url}}/simplify/table/{{expression}}?hide=FALSE
> {%
client.test("Response body does not contain false values", () => {
const table = response.body.truthTable;
const results = table.truthMatrix.map(arr => arr[arr.length - 1])
for (let i = 0; i < results.length; i++) {
client.assert(results[i] === true, "Response body contains false values")
}
});
%}

View File

@ -41,10 +41,9 @@ pub struct TruthTableOptions {
}
impl TruthTable {
// TODO hide option
pub fn new(expression: &Expression, options: TruthTableOptions) -> Self {
let header = Self::extract_header(expression);
let mut truth_matrix = Self::generate_truth_matrix(expression, &header);
let mut truth_matrix = Self::generate_truth_matrix(expression, &header, options.hide);
if !matches!(options.sort, Sort::Default) {
Self::sort_matrix(&mut truth_matrix, options.sort);
}
@ -91,7 +90,7 @@ impl TruthTable {
}
}
fn generate_truth_matrix(expression: &Expression, header: &[String]) -> TruthMatrix {
fn generate_truth_matrix(expression: &Expression, header: &[String], hide: Hide) -> TruthMatrix {
let mut atomics = expression.get_atomic_values()
.into_iter().collect::<Vec<String>>();
if atomics.is_empty() {
@ -99,11 +98,17 @@ impl TruthTable {
}
atomics.sort();
Self::truth_combinations(atomics.len()).iter()
.map(|combo| {
Self::resolve_expression(expression, &atomics.iter()
.filter_map(|combo| {
let expression = Self::resolve_expression(expression, &atomics.iter()
.enumerate()
.map(|(index, value)| (value.clone(), combo[index]))
.collect(), header)
.collect(), header);
match (hide, expression.last()) {
(Hide::True, Some(false)) => Some(expression),
(Hide::False, Some(true)) => Some(expression),
(Hide::None, _) => Some(expression),
_ => None,
}
}).collect()
}
@ -269,6 +274,50 @@ mod tests {
]);
}
#[test]
fn test_hide_true_values() {
let expected = matrix![
true, false, false;
false, true, false;
false, false, false
];
let matrix = TruthTable::generate_truth_matrix(
&and(atomic("A"), atomic("B")),
&["A".into(), "B".into(), "A ⋀ B".into()],
Hide::True,
);
assert_eq!(expected, matrix);
}
#[test]
fn test_hide_false_values() {
let expected = matrix![
true, true, true
];
let matrix = TruthTable::generate_truth_matrix(
&and(atomic("A"), atomic("B")),
&["A".into(), "B".into(), "A ⋀ B".into()],
Hide::False,
);
assert_eq!(expected, matrix);
}
#[test]
fn test_hide_none() {
let expected = matrix![
true, true, true;
true, false, false;
false, true, false;
false, false, false
];
let matrix = TruthTable::generate_truth_matrix(
&and(atomic("A"), atomic("B")),
&["A".into(), "B".into(), "A ⋀ B".into()],
Hide::None,
);
assert_eq!(expected, matrix);
}
#[test]
fn test_truth_combinations_2() {
let combinations = TruthTable::truth_combinations(2);