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 f4daa7b
Showing 1 changed file with 98 additions and 1 deletion.
99 changes: 98 additions & 1 deletion 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 @@ -398,7 +403,7 @@ mod tests {
use crate::assert_approx_eq;

use super::*;
use std::iter::FromIterator;
use std::{cmp::Ordering, iter::FromIterator};
use tempfile::TempDir;

static HACKATOM: &[u8] = include_bytes!("../../testdata/hackatom.wasm");
Expand Down Expand Up @@ -772,6 +777,98 @@ 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 with numeric ordering
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
enum Number {
One = 1,
Two = 2,
Three = 3,
Eleven = 11,
Twelve = 12,
Zero = 0,
MinusOne = -1,
}

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

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

// custom type with lexicographical ordering
#[derive(PartialEq, Eq)]
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 Ord for Color {
fn cmp(&self, other: &Self) -> Ordering {
// sort by name
self.as_ref().cmp(other.as_ref())
}
}

impl PartialOrd for Color {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

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 f4daa7b

Please sign in to comment.