Skip to content

Commit 33959dc

Browse files
committed
Use BTreeMap and BTreeSet instead of HashMap and HashSet
1 parent ec52e60 commit 33959dc

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

runtimes/rust/lbr-prelude/src/generators/correct.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use num_bigint::{BigInt, Sign};
22
use proptest::arbitrary::{any, StrategyFor};
33
use proptest::char::CharStrategy;
44
use proptest::collection::vec;
5-
use proptest::collection::{hash_map, hash_set};
5+
use proptest::collection::{btree_map, btree_set};
66
use proptest::option;
77
use proptest::prelude::{prop_oneof, Just};
88
use proptest::result::maybe_err;
99
use proptest::strategy::Strategy;
10-
use std::collections::{HashMap, HashSet};
10+
use std::collections::{BTreeMap, BTreeSet};
1111

1212
/// Strategy to generate an arbitrary boolean
1313
pub fn arb_bool() -> StrategyFor<bool> {
@@ -47,11 +47,12 @@ pub fn arb_text() -> StrategyFor<String> {
4747

4848
/// Strategy to generate a complicated data structure
4949
pub fn arb_complicated(
50-
) -> impl Strategy<Value = HashMap<String, Result<HashSet<char>, Option<Result<Vec<u8>, bool>>>>> {
51-
hash_map(
50+
) -> impl Strategy<Value = BTreeMap<String, Result<BTreeSet<char>, Option<Result<Vec<u8>, bool>>>>>
51+
{
52+
btree_map(
5253
arb_text(),
5354
maybe_err(
54-
hash_set(arb_char(), 20),
55+
btree_set(arb_char(), 20),
5556
option::of(maybe_err(arb_bytes(), arb_bool())),
5657
),
5758
20,

runtimes/rust/lbr-prelude/src/json.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use core::str::FromStr;
33
use data_encoding::BASE64;
44
use num_bigint::BigInt;
55
use serde_json::{self, Value};
6-
use std::collections::{HashMap, HashSet};
7-
use std::hash::Hash;
6+
use std::collections::{BTreeMap, BTreeSet};
87

98
/// Trait that lbf-prelude::json class maps to
109
pub trait Json {
@@ -209,9 +208,9 @@ where
209208
}
210209
}
211210

212-
impl<T> Json for HashSet<T>
211+
impl<T> Json for BTreeSet<T>
213212
where
214-
T: Json + Eq + Hash,
213+
T: Json + Eq + Ord,
215214
{
216215
fn to_json(&self) -> Result<Value, Error> {
217216
let values = self
@@ -227,7 +226,7 @@ where
227226
let set = vec
228227
.iter()
229228
.map(|val| T::from_json(val.clone()))
230-
.collect::<Result<HashSet<T>, Error>>()?;
229+
.collect::<Result<BTreeSet<T>, Error>>()?;
231230

232231
if set.len() == vec.len() {
233232
Ok(set)
@@ -241,9 +240,9 @@ where
241240
}
242241
}
243242

244-
impl<K, V> Json for HashMap<K, V>
243+
impl<K, V> Json for BTreeMap<K, V>
245244
where
246-
K: Json + Eq + Hash,
245+
K: Json + Eq + Ord,
247246
V: Json,
248247
{
249248
fn to_json(&self) -> Result<Value, Error> {
@@ -260,7 +259,7 @@ where
260259
let set = vec
261260
.iter()
262261
.map(|kv_tuple| <(K, V)>::from_json(kv_tuple.clone()))
263-
.collect::<Result<HashMap<K, V>, Error>>()?;
262+
.collect::<Result<BTreeMap<K, V>, Error>>()?;
264263

265264
if set.len() == vec.len() {
266265
Ok(set)

runtimes/rust/lbr-prelude/tests/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod tests {
66
arb_bool, arb_bytes, arb_char, arb_complicated, arb_integer, arb_text,
77
};
88
use lbr_prelude::json::Json;
9-
use proptest::collection::{hash_map, hash_set, vec};
9+
use proptest::collection::{btree_map, btree_set, vec};
1010
use proptest::option;
1111
use proptest::prelude::*;
1212
use proptest::result::maybe_err;
@@ -76,14 +76,14 @@ mod tests {
7676

7777
proptest! {
7878
#[test]
79-
fn test_set(val in hash_set(arb_integer(), 20)) {
79+
fn test_set(val in btree_set(arb_integer(), 20)) {
8080
assert_eq!(val, from_to_json(&val)?);
8181
}
8282
}
8383

8484
proptest! {
8585
#[test]
86-
fn test_map(val in hash_map(arb_integer(), arb_text(), 20)) {
86+
fn test_map(val in btree_map(arb_integer(), arb_text(), 20)) {
8787
assert_eq!(val, from_to_json(&val)?);
8888
}
8989
}

0 commit comments

Comments
 (0)