Skip to content

Commit

Permalink
Elaborate on set_to_csv and att tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Jan 4, 2024
1 parent 68a5137 commit 44e93dc
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions libwasmvm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ impl From<cosmwasm_vm::AnalysisReport> for AnalysisReport {
}
}

/// Takes a set of string-like elements and returns a comma-separated list.
/// Since no escaping or quoting is applied to the elements, the caller needs to ensure
/// only simple alphanumeric values are used.
///
/// The order of the output elements is determined by the iteration order of the provided set.
fn set_to_csv(set: BTreeSet<impl AsRef<str>>) -> String {
let list: Vec<&str> = set.iter().map(|e| e.as_ref()).collect();
list.join(",")
Expand Down Expand Up @@ -772,6 +777,92 @@ mod tests {
])),
"A,AA,B,C,a,aa,b,c",
);

// str
assert_eq!(
set_to_csv(BTreeSet::from_iter(vec![
"a", "aa", "b", "c", "A", "AA", "B", "C",
])),
"A,AA,B,C,a,aa,b,c",
);

// custom type
#[derive(PartialEq, Eq, Ord, Clone, Copy)]
enum Number {
One = 1,
Two = 2,
Three = 3,
Eleven = 11,
Twelve = 12,
}

impl AsRef<str> for Number {
fn as_ref(&self) -> &str {
use Number::*;
match self {
One => "1",
Two => "2",
Three => "3",
Eleven => "11",
Twelve => "12",
}
}
}

impl PartialOrd for Number {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
// sort by value
(*self as i32).partial_cmp(&(*other as i32))
}
}

assert_eq!(
set_to_csv(BTreeSet::from_iter([
Number::One,
Number::Two,
Number::Three,
Number::Eleven,
Number::Twelve,
])),
"1,2,3,11,12",
);

#[derive(PartialEq, Eq, Ord)]
enum Color {
Red,
Green,
Blue,
Yellow,
}

impl AsRef<str> for Color {
fn as_ref(&self) -> &str {
use Color::*;
match self {
Red => "red",
Green => "green",
Blue => "blue",
Yellow => "yellow",
}
}
}

impl PartialOrd for Color {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
// sort by name
self.as_ref().partial_cmp(other.as_ref())
}
}

assert_eq!(
set_to_csv(BTreeSet::from_iter([
Color::Red,
Color::Green,
Color::Blue,
Color::Yellow,
])),
"blue,green,red,yellow",
);
}

#[test]
Expand Down

0 comments on commit 44e93dc

Please sign in to comment.