Skip to content

Commit

Permalink
Reorganize a bit the test files
Browse files Browse the repository at this point in the history
  • Loading branch information
sonmarcho committed Nov 29, 2023
1 parent 215e601 commit be6418b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 67 deletions.
5 changes: 4 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ charon-tests: \
test-loops test-loops_cfg test-hashmap \
test-paper test-hashmap_main \
test-matches test-matches_duplicate test-external \
test-constants test-array test-array_const_generics test-traits
test-constants test-array test-array_const_generics test-traits \
test-closures

test-nested_borrows: OPTIONS += --no-code-duplication
test-no_nested_borrows: OPTIONS += --no-code-duplication
Expand All @@ -42,6 +43,8 @@ test-matches_duplicate:
test-array:
test-array_const_generics:
test-traits:
test-traits_special:
test-closures:

# =============================================================================
# The tests.
Expand Down
109 changes: 109 additions & 0 deletions tests/src/closures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*pub fn incr_u32(x: u32) -> u32 {
x + 1
}
/*
/* Testing function pointers and closures */
// TODO: this requires to take into account the constraints over associated types
// because the output type of the Fn trait is an associated type, not a parameter.
// More precisely, we have the constraint that:
// <F as Fn<T>>::Output = T
#[allow(clippy::manual_map)]
pub fn map_option<T, F>(x: Option<T>, f: F) -> Option<T>
where
F: Fn(T) -> T,
{
match x {
None => None,
Some(x) => Some(f(x)),
}
}
*/
// With a pointer to a top level function
pub fn test_map_option1(x: Option<u32>) -> Option<u32> {
map_option(x, incr_u32)
}
*/

/*
//
pub fn test_closure1<T>(x: &T) -> &T {
//let f: fn(&T) -> &T = |x| x;
let f: fn(u32) -> u32 = |x| x;
//(f)(x)
x
}
*/

/*
// With a local function
pub fn test_map_option2(x: Option<u32>) -> Option<u32> {
let f: fn(u32) -> u32 = |x| x + 1;
map_option(x, f)
}
/*
// With a local function which uses local type variables
pub fn test_map_option3<T, U>(_: T, x: Option<U>) -> Option<U> {
let f: fn(U) -> U = |x| x;
map_option(x, f)
}
*/
/*// With a `dyn`
pub fn test_map_option3(x: Option<u32>) -> Option<u32> {
let f: fn(u32) -> u32 = |x| x + 1;
map_option(x, f)
}*/
/*
pub fn map(x: [i32; 256]) -> [i32; 256] {
x.map(|v| v)
}
*/
*/

/*
macro_rules! impl_generic_struct {
($name:ident) => {
pub struct $name<const SIZE: usize> {
pub value: [u8; SIZE],
}
impl<const SIZE: usize> TryFrom<&[u8]> for $name<SIZE> {
type Error = core::array::TryFromSliceError;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value.try_into() {
Ok(value) => Ok(Self { value }),
Err(e) => Err(e),
}
}
}
};
}
impl_generic_struct!(KyberCiphertext);
*/

/*
use std::convert::{TryFrom, TryInto};
pub struct KyberCypherText<const SIZE: usize> {
pub value: [u8; SIZE],
}
impl<const SIZE: usize> TryFrom<&[u8]> for KyberCypherText<SIZE> {
type Error = core::array::TryFromSliceError;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value.try_into() {
Ok(value) => Ok(Self { value }),
Err(e) => Err(e),
}
}
}
*/
2 changes: 2 additions & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod array;
pub mod array_const_generics;
pub mod closures;
pub mod constants;
pub mod external;
pub mod hashmap;
Expand All @@ -10,3 +11,4 @@ pub mod nested_borrows;
pub mod no_nested_borrows;
pub mod paper;
pub mod traits;
pub mod traits_special;
66 changes: 0 additions & 66 deletions tests/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,69 +296,3 @@ pub trait CFnMut<Args>: CFnOnce<Args> {
pub trait CFn<Args>: CFnMut<Args> {
fn call_mut(&self, args: Args) -> Self::Output;
}

pub fn incr_u32(x: u32) -> u32 {
x + 1
}

/*
/* Testing function pointers and closures */
// TODO: this requires to take into account the constraints over associated types
// because the output type of the Fn trait is an associated type, not a parameter.
// More precisely, we have the constraint that:
// <F as Fn<T>>::Output = T
#[allow(clippy::manual_map)]
pub fn map_option<T, F>(x: Option<T>, f: F) -> Option<T>
where
F: Fn(T) -> T,
{
match x {
None => None,
Some(x) => Some(f(x)),
}
}
// With a pointer to a top level function
pub fn test_map_option1(x: Option<u32>) -> Option<u32> {
map_option(x, incr_u32)
}*/

/*
//
pub fn test_closure1<T>(x: &T) -> &T {
//let f: fn(&T) -> &T = |x| x;
let f: fn(u32) -> u32 = |x| x;
//(f)(x)
x
}
*/

/*
// With a local function
pub fn test_map_option2(x: Option<u32>) -> Option<u32> {
let f: fn(u32) -> u32 = |x| x + 1;
map_option(x, f)
}
/*
// With a local function which uses local type variables
pub fn test_map_option3<T, U>(_: T, x: Option<U>) -> Option<U> {
let f: fn(U) -> U = |x| x;
map_option(x, f)
}
*/
/*// With a `dyn`
pub fn test_map_option3(x: Option<u32>) -> Option<u32> {
let f: fn(u32) -> u32 = |x| x + 1;
map_option(x, f)
}*/
/*
pub fn map(x: [i32; 256]) -> [i32; 256] {
x.map(|v| v)
}
*/
*/
26 changes: 26 additions & 0 deletions tests/src/traits_special.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// The example below (in particular the impl block) is peculiar with regards
// to the treatment of regions. In particular, when we translate the implementation
// of `from` to LLBC, we get something which looks like this:
// ```
//
// fn crate::{bool}::from<@R0, @R1>(@1: &@R1 (bool)) ->
// core::result::Result<bool, crate::{bool}<@R0>::Error> {
// // ^^^
// // HERE
// ... // omitted
// }
// ```
pub trait From<T> {
type Error;
fn from(v: T) -> Result<Self, Self::Error>
where
Self: std::marker::Sized;
}

impl From<&bool> for bool {
type Error = ();

fn from(v: &bool) -> Result<Self, Self::Error> {
Ok(*v)
}
}

0 comments on commit be6418b

Please sign in to comment.