Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeul-zama committed Dec 2, 2024
1 parent a224e66 commit 9fe1dc5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 69 deletions.
26 changes: 10 additions & 16 deletions tfhe/src/strings/test_functions/test_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@ where
P: Into<PBSParameters>,
{
let executor = CpuFunctionExecutor::new(&ServerKey::is_empty);
string_is_empty_test_impl(param, executor, |a| a.is_empty());
string_is_empty_test_impl(param, executor);
}

pub(crate) fn string_is_empty_test_impl<P, T>(
param: P,
mut is_empty_executor: T,
clear_function: for<'a> fn(&'a str) -> bool,
) where
pub(crate) fn string_is_empty_test_impl<P, T>(param: P, mut is_empty_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<&'a FheString, FheStringIsEmpty>,
{
Expand All @@ -63,7 +60,7 @@ pub(crate) fn string_is_empty_test_impl<P, T>(
// trivial
for str in ["", "a", "abc"] {
for pad in 0..3 {
let expected_result = clear_function(str);
let expected_result = str.is_empty();

let enc_str = FheString::new_trivial(&cks, str, Some(pad));

Expand All @@ -82,7 +79,7 @@ pub(crate) fn string_is_empty_test_impl<P, T>(
let pad = 1;

for str in ["", "abc"] {
let expected_result = clear_function(str);
let expected_result = str.is_empty();

let enc_str = FheString::new(&cks, str, Some(pad));

Expand All @@ -109,14 +106,11 @@ where
P: Into<PBSParameters>,
{
let executor = CpuFunctionExecutor::new(&ServerKey::len);
string_len_test_impl(param, executor, |a| a.len());
string_len_test_impl(param, executor);
}

pub(crate) fn string_len_test_impl<P, T>(
param: P,
mut len_executor: T,
clear_function: for<'a> fn(&'a str) -> usize,
) where
pub(crate) fn string_len_test_impl<P, T>(param: P, mut len_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<&'a FheString, FheStringLen>,
{
Expand All @@ -129,7 +123,7 @@ pub(crate) fn string_len_test_impl<P, T>(
// trivial
for str in ["", "a", "abc"] {
for pad in 0..3 {
let expected_result = clear_function(str);
let expected_result = str.len();

let enc_str = FheString::new_trivial(&cks, str, Some(pad));

Expand All @@ -150,7 +144,7 @@ pub(crate) fn string_len_test_impl<P, T>(
let pad = 1;

for str in ["", "abc"] {
let expected_result = clear_function(str);
let expected_result = str.len();

let enc_str = FheString::new(&cks, str, Some(pad));

Expand Down
26 changes: 10 additions & 16 deletions tfhe/src/strings/test_functions/test_concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ where
P: Into<PBSParameters>,
{
let executor = CpuFunctionExecutor::new(&ServerKey::concat);
string_concat_test_impl(param, executor, |lhs, rhs| lhs.to_owned() + rhs);
string_concat_test_impl(param, executor);
}

pub(crate) fn string_concat_test_impl<P, T>(
param: P,
mut concat_executor: T,
clear_function: for<'a> fn(&'a str, &'a str) -> String,
) where
pub(crate) fn string_concat_test_impl<P, T>(param: P, mut concat_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<(&'a FheString, &'a FheString), FheString>,
{
Expand All @@ -42,7 +39,7 @@ pub(crate) fn string_concat_test_impl<P, T>(
for rhs_pad in 0..2 {
for str in TEST_CASES_CONCAT {
for rhs in TEST_CASES_CONCAT {
let expected_result = clear_function(str, rhs);
let expected_result = str.to_owned() + rhs;

let enc_lhs = FheString::new_trivial(&cks, str, Some(str_pad));
let enc_rhs = FheString::new_trivial(&cks, rhs, Some(rhs_pad));
Expand All @@ -61,7 +58,7 @@ pub(crate) fn string_concat_test_impl<P, T>(
let rhs = "b";
let rhs_pad = 1;

let expected_result = clear_function(str, rhs);
let expected_result = str.to_owned() + rhs;

let enc_lhs = FheString::new(&cks, str, Some(str_pad));
let enc_rhs = FheString::new(&cks, rhs, Some(rhs_pad));
Expand All @@ -83,14 +80,11 @@ where
P: Into<PBSParameters>,
{
let executor = CpuFunctionExecutor::new(&ServerKey::repeat);
string_repeat_test_impl(param, executor, |lhs, n| lhs.repeat(n as usize));
string_repeat_test_impl(param, executor);
}

pub(crate) fn string_repeat_test_impl<P, T>(
param: P,
mut repeat_executor: T,
clear_function: for<'a> fn(&'a str, u16) -> String,
) where
pub(crate) fn string_repeat_test_impl<P, T>(param: P, mut repeat_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<(&'a FheString, &'a UIntArg), FheString>,
{
Expand All @@ -105,7 +99,7 @@ pub(crate) fn string_repeat_test_impl<P, T>(
for n in 0..3 {
for str in TEST_CASES_CONCAT {
for max in n..n + 2 {
let expected_result = clear_function(str, n);
let expected_result = str.repeat(n as usize);

let enc_str = FheString::new_trivial(&cks, str, Some(str_pad));

Expand All @@ -129,7 +123,7 @@ pub(crate) fn string_repeat_test_impl<P, T>(
let n = 1;
let max = 2;

let expected_result = clear_function(str, n);
let expected_result = str.repeat(n as usize);

let enc_str = FheString::new(&cks, str, Some(str_pad));

Expand Down
28 changes: 10 additions & 18 deletions tfhe/src/strings/test_functions/test_find_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,11 @@ where
P: Into<PBSParameters>,
{
let executor = CpuFunctionExecutor::new(&ServerKey::replace);
string_replace_test_impl(param, executor, |str, from, to| str.replace(from, to));
string_replace_test_impl(param, executor);
}

pub(crate) fn string_replace_test_impl<P, T>(
param: P,
mut replace_executor: T,
clear_function: for<'a> fn(&'a str, &'a str, &'a str) -> String,
) where
pub(crate) fn string_replace_test_impl<P, T>(param: P, mut replace_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<(&'a FheString, GenericPatternRef<'a>, &'a FheString), FheString>,
{
Expand All @@ -145,7 +142,7 @@ pub(crate) fn string_replace_test_impl<P, T>(
for str in TEST_CASES_FIND {
for from in PATTERN_FIND {
for to in ["", " ", "a", "abc"] {
let expected_result = clear_function(str, from, to);
let expected_result = str.replace(from, to);

let enc_str = FheString::new_trivial(&cks, str, Some(str_pad));
let enc_from = GenericPattern::Enc(FheString::new_trivial(
Expand Down Expand Up @@ -181,7 +178,7 @@ pub(crate) fn string_replace_test_impl<P, T>(
let to_pad = 1;

for from in ["a", "c"] {
let expected_result = clear_function(str, from, to);
let expected_result = str.replace(from, to);

let enc_str = FheString::new_trivial(&cks, str, Some(str_pad));
let enc_from = GenericPattern::Enc(FheString::new_trivial(&cks, from, Some(from_pad)));
Expand Down Expand Up @@ -211,16 +208,11 @@ where
P: Into<PBSParameters>,
{
let executor = CpuFunctionExecutor::new(&ServerKey::replacen);
string_replacen_test_impl(param, executor, |str, from, to, count| {
str.replacen(from, to, count as usize)
});
string_replacen_test_impl(param, executor);
}

pub(crate) fn string_replacen_test_impl<P, T>(
param: P,
mut replacen_executor: T,
clear_function: for<'a> fn(&'a str, &'a str, &'a str, u16) -> String,
) where
pub(crate) fn string_replacen_test_impl<P, T>(param: P, mut replacen_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<
(
Expand All @@ -247,7 +239,7 @@ pub(crate) fn string_replacen_test_impl<P, T>(
for to in ["", " ", "a", "abc"] {
for n in 0..=2 {
for max in n..n + 2 {
let expected_result = clear_function(str, from, to, n);
let expected_result = str.replacen(from, to, n as usize);

let enc_str = FheString::new_trivial(&cks, str, Some(str_pad));
let enc_from = GenericPattern::Enc(FheString::new_trivial(
Expand Down Expand Up @@ -296,7 +288,7 @@ pub(crate) fn string_replacen_test_impl<P, T>(
let max = 2;

for from in ["a", "c"] {
let expected_result = clear_function(str, from, to, n);
let expected_result = str.replacen(from, to, n as usize);

let enc_str = FheString::new_trivial(&cks, str, Some(str_pad));
let enc_from = GenericPattern::Enc(FheString::new_trivial(&cks, from, Some(from_pad)));
Expand Down
13 changes: 5 additions & 8 deletions tfhe/src/strings/test_functions/test_up_low_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,11 @@ where
P: Into<PBSParameters>,
{
let executor = CpuFunctionExecutor::new(&ServerKey::eq_ignore_case);
string_eq_ignore_case_test_impl(param, executor, |lhs, rhs| lhs.eq_ignore_ascii_case(rhs));
string_eq_ignore_case_test_impl(param, executor);
}

pub(crate) fn string_eq_ignore_case_test_impl<P, T>(
param: P,
mut eq_ignore_case_executor: T,
clear_function: for<'a> fn(&'a str, &'a str) -> bool,
) where
pub(crate) fn string_eq_ignore_case_test_impl<P, T>(param: P, mut eq_ignore_case_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<(&'a FheString, GenericPatternRef<'a>), BooleanBlock>,
{
Expand All @@ -119,7 +116,7 @@ pub(crate) fn string_eq_ignore_case_test_impl<P, T>(
for rhs_pad in 0..2 {
for str in UP_LOW_CASE {
for rhs in UP_LOW_CASE {
let expected_result = clear_function(str, rhs);
let expected_result = str.eq_ignore_ascii_case(rhs);

let enc_str = FheString::new(&cks, str, Some(str_pad));

Expand All @@ -143,7 +140,7 @@ pub(crate) fn string_eq_ignore_case_test_impl<P, T>(
let rhs_pad = 1;

for rhs in ["Ab", "Ac"] {
let expected_result = clear_function(str, rhs);
let expected_result = str.eq_ignore_ascii_case(rhs);

let enc_str = FheString::new(&cks, str, Some(str_pad));
let enc_rhs = GenericPattern::Enc(FheString::new_trivial(&cks, rhs, Some(rhs_pad)));
Expand Down
24 changes: 13 additions & 11 deletions tfhe/src/strings/test_functions/test_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,11 @@ where

let executor = CpuFunctionExecutor::new(&fhe_func);

let func: for<'a> fn(&'a str) -> Box<dyn Iterator<Item = &'a str> + 'a> =
|lhs| Box::new(lhs.split_ascii_whitespace());

string_split_whitespace_test_impl(param, executor, func);
string_split_whitespace_test_impl(param, executor);
}

pub(crate) fn string_split_whitespace_test_impl<P, T>(
param: P,
mut split_whitespace_executor: T,
clear_function: for<'a> fn(&'a str) -> Box<dyn Iterator<Item = &'a str> + 'a>,
) where
pub(crate) fn string_split_whitespace_test_impl<P, T>(param: P, mut split_whitespace_executor: T)
where
P: Into<PBSParameters>,
T: for<'a> FunctionExecutor<&'a FheString, Box<dyn FheStringIterator>>,
{
Expand Down Expand Up @@ -145,7 +139,11 @@ pub(crate) fn string_split_whitespace_test_impl<P, T>(
format!("{ws}a{ws}a"),
format!("a{ws}a{ws}a"),
] {
let expected: Vec<_> = clear_function(&str).map(Some).chain(once(None)).collect();
let expected: Vec<_> = str
.split_ascii_whitespace()
.map(Some)
.chain(once(None))
.collect();

let enc_str = FheString::new(&cks, &str, Some(str_pad));

Expand All @@ -170,7 +168,11 @@ pub(crate) fn string_split_whitespace_test_impl<P, T>(
let str_pad = 1;

for str in ["a b", "abc"] {
let expected: Vec<_> = clear_function(str).map(Some).chain(once(None)).collect();
let expected: Vec<_> = str
.split_ascii_whitespace()
.map(Some)
.chain(once(None))
.collect();

let enc_str = FheString::new(&cks, str, Some(str_pad));

Expand Down

0 comments on commit 9fe1dc5

Please sign in to comment.