Skip to content

Commit 4f6aa36

Browse files
author
Gianmarco Garrisi
committed
Enable serde tests for no-std
1 parent 50d58cb commit 4f6aa36

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ serde = { version = "1", features = ["derive"] }
2323
serde_json = "1"
2424
uuid = {version= "1", features = ["v4", "serde"] }
2525
hashbrown = "0.14"
26+
twox-hash = { version = "1.5", default-features = false }
2627

2728
[features]
2829
default = ["std"]

tests/double_priority_queue.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,16 +1260,28 @@ mod doublepq_tests {
12601260
mod serde_tests_basics {
12611261
use priority_queue::DoublePriorityQueue;
12621262
use serde_test::{assert_tokens, Token};
1263+
1264+
#[cfg(not(feature = "std"))]
1265+
use twox_hash::XxHash64;
1266+
#[cfg(not(feature = "std"))]
1267+
use core::hash::BuildHasherDefault;
1268+
12631269
#[test]
12641270
fn serde_empty() {
1271+
#[cfg(feature = "std")]
12651272
let pq: DoublePriorityQueue<String, i32> = DoublePriorityQueue::new();
1273+
#[cfg(not(feature = "std"))]
1274+
let pq: DoublePriorityQueue<String, i32, BuildHasherDefault<XxHash64>> = DoublePriorityQueue::default();
12661275

12671276
assert_tokens(&pq, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
12681277
}
12691278

12701279
#[test]
12711280
fn serde() {
1281+
#[cfg(feature = "std")]
12721282
let mut pq = DoublePriorityQueue::new();
1283+
#[cfg(not(feature = "std"))]
1284+
let mut pq: DoublePriorityQueue<&str, i32, BuildHasherDefault<XxHash64>> = DoublePriorityQueue::default();
12731285

12741286
pq.push("a", 1);
12751287
pq.push("b", 2);
@@ -1316,6 +1328,11 @@ mod serde_tests_custom_structs {
13161328
use std::default::Default;
13171329
use std::time::Duration;
13181330
use uuid::Uuid;
1331+
1332+
#[cfg(not(feature = "std"))]
1333+
use twox_hash::XxHash64;
1334+
#[cfg(not(feature = "std"))]
1335+
use core::hash::BuildHasherDefault;
13191336

13201337
use serde::{Deserialize, Serialize};
13211338

@@ -1410,9 +1427,12 @@ mod serde_tests_custom_structs {
14101427
fn test1() {
14111428
println!("test1()");
14121429

1430+
#[cfg(feature = "std")]
14131431
type PqType = DoublePriorityQueue<i32, i32>;
1432+
#[cfg(not(feature = "std"))]
1433+
type PqType = DoublePriorityQueue<i32, i32, BuildHasherDefault<XxHash64>>;
14141434

1415-
let mut pq: PqType = DoublePriorityQueue::new();
1435+
let mut pq: PqType = DoublePriorityQueue::default();
14161436
pq.push(0, 0);
14171437
pq.push(1, 1);
14181438

@@ -1429,9 +1449,12 @@ mod serde_tests_custom_structs {
14291449
fn test2() {
14301450
println!("\n\ntest2()");
14311451

1452+
#[cfg(feature = "std")]
14321453
type PqType = DoublePriorityQueue<i32, EventComparables>;
1454+
#[cfg(not(feature = "std"))]
1455+
type PqType = DoublePriorityQueue<i32, EventComparables, BuildHasherDefault<XxHash64>>;
14331456

1434-
let mut pq: PqType = DoublePriorityQueue::new();
1457+
let mut pq: PqType = DoublePriorityQueue::default();
14351458
pq.push(0, Default::default()); // Uuids will be different
14361459
pq.push(1, Default::default());
14371460

@@ -1488,9 +1511,12 @@ mod serde_tests_custom_structs {
14881511
assert_eq!(ec2, deserialized);
14891512

14901513
{
1514+
#[cfg(feature = "std")]
14911515
type PqType = DoublePriorityQueue<ConcreteEvent1, EventComparables>;
1516+
#[cfg(not(feature = "std"))]
1517+
type PqType = DoublePriorityQueue<ConcreteEvent1, EventComparables, BuildHasherDefault<XxHash64>>;
14921518

1493-
let mut pq: PqType = DoublePriorityQueue::new();
1519+
let mut pq: PqType = DoublePriorityQueue::default();
14941520
pq.push(ce1, ec1);
14951521
pq.push(ce2, ec2);
14961522

tests/priority_queue.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,16 +1076,28 @@ mod pqueue_tests {
10761076
mod serde_tests_basics {
10771077
use priority_queue::PriorityQueue;
10781078
use serde_test::{assert_tokens, Token};
1079+
1080+
#[cfg(not(feature = "std"))]
1081+
use twox_hash::XxHash64;
1082+
#[cfg(not(feature = "std"))]
1083+
use core::hash::BuildHasherDefault;
1084+
10791085
#[test]
10801086
fn serde_empty() {
1087+
#[cfg(feature = "std")]
10811088
let pq: PriorityQueue<String, i32> = PriorityQueue::new();
1089+
#[cfg(not(feature = "std"))]
1090+
let pq: PriorityQueue<String, i32, BuildHasherDefault<XxHash64>> = PriorityQueue::default();
10821091

10831092
assert_tokens(&pq, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
10841093
}
10851094

10861095
#[test]
10871096
fn serde() {
1097+
#[cfg(feature = "std")]
10881098
let mut pq = PriorityQueue::new();
1099+
#[cfg(not(feature = "std"))]
1100+
let mut pq: PriorityQueue<&str, i32, BuildHasherDefault<XxHash64>> = PriorityQueue::default();
10891101

10901102
pq.push("a", 1);
10911103
pq.push("b", 2);
@@ -1128,10 +1140,15 @@ mod serde_tests_basics {
11281140
#[cfg(all(feature = "serde", test))]
11291141
mod serde_tests_custom_structs {
11301142
use priority_queue::PriorityQueue;
1131-
use std::cmp::{Ord, Ordering, PartialOrd};
1132-
use std::default::Default;
1133-
use std::time::Duration;
1143+
use core::cmp::{Ord, Ordering, PartialOrd};
1144+
use core::default::Default;
1145+
use core::time::Duration;
11341146
use uuid::Uuid;
1147+
1148+
#[cfg(not(feature = "std"))]
1149+
use twox_hash::XxHash64;
1150+
#[cfg(not(feature = "std"))]
1151+
use core::hash::BuildHasherDefault;
11351152

11361153
use serde::{Deserialize, Serialize};
11371154

@@ -1226,9 +1243,12 @@ mod serde_tests_custom_structs {
12261243
fn test1() {
12271244
println!("test1()");
12281245

1246+
#[cfg(feature = "std")]
12291247
type PqType = PriorityQueue<i32, i32>;
1248+
#[cfg(not(feature = "std"))]
1249+
type PqType = PriorityQueue<i32, i32, BuildHasherDefault<XxHash64>>;
12301250

1231-
let mut pq: PqType = PriorityQueue::new();
1251+
let mut pq: PqType = PriorityQueue::default();
12321252
pq.push(0, 0);
12331253
pq.push(1, 1);
12341254

@@ -1245,9 +1265,12 @@ mod serde_tests_custom_structs {
12451265
fn test2() {
12461266
println!("\n\ntest2()");
12471267

1268+
#[cfg(feature = "std")]
12481269
type PqType = PriorityQueue<i32, EventComparables>;
1270+
#[cfg(not(feature = "std"))]
1271+
type PqType = PriorityQueue<i32, EventComparables, BuildHasherDefault<XxHash64>>;
12491272

1250-
let mut pq: PqType = PriorityQueue::new();
1273+
let mut pq: PqType = PriorityQueue::default();
12511274
pq.push(0, Default::default()); // Uuids will be different
12521275
pq.push(1, Default::default());
12531276

@@ -1304,9 +1327,12 @@ mod serde_tests_custom_structs {
13041327
assert_eq!(ec2, deserialized);
13051328

13061329
{
1330+
#[cfg(feature = "std")]
13071331
type PqType = PriorityQueue<ConcreteEvent1, EventComparables>;
1332+
#[cfg(not(feature = "std"))]
1333+
type PqType = PriorityQueue<ConcreteEvent1, EventComparables, BuildHasherDefault<XxHash64>>;
13081334

1309-
let mut pq: PqType = PriorityQueue::new();
1335+
let mut pq: PqType = PriorityQueue::default();
13101336
pq.push(ce1, ec1);
13111337
pq.push(ce2, ec2);
13121338

0 commit comments

Comments
 (0)