Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit f368608

Browse files
committed
Enable running tests without an allocator
Currently we always run tests with "std" enabled. It is currently not possible to run the tests without an allocator but there is no reason that we should not be able to run allocation free tests without an allocator, doing so is better because it tests our claim that the lib can be used without an allocator.
1 parent f6603c2 commit f368608

File tree

9 files changed

+89
-80
lines changed

9 files changed

+89
-80
lines changed

src/hash160.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,20 @@ impl HashTrait for Hash {
104104

105105
#[cfg(test)]
106106
mod tests {
107-
use hash160;
108-
use hex::{FromHex, ToHex};
109-
use Hash;
110-
use HashEngine;
111-
112-
#[derive(Clone)]
113-
struct Test {
114-
input: Vec<u8>,
115-
output: Vec<u8>,
116-
output_str: &'static str,
117-
}
118-
119107
#[test]
108+
#[cfg(any(feature = "std", feature = "alloc"))]
120109
fn test() {
110+
use {hash160, Hash, HashEngine};
111+
use hex::{FromHex, ToHex};
112+
113+
#[derive(Clone)]
114+
#[cfg(any(feature = "std", feature = "alloc"))]
115+
struct Test {
116+
input: Vec<u8>,
117+
output: Vec<u8>,
118+
output_str: &'static str,
119+
}
120+
121121
let tests = vec![
122122
// Uncompressed pubkey obtained from Bitcoin key; data from validateaddress
123123
Test {
@@ -161,8 +161,8 @@ mod tests {
161161
#[cfg(feature = "serde")]
162162
#[test]
163163
fn ripemd_serde() {
164-
165164
use serde_test::{Configure, Token, assert_tokens};
165+
use {hash160, Hash};
166166

167167
static HASH_BYTES: [u8; 20] = [
168168
0x13, 0x20, 0x72, 0xdf,

src/hex.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ mod tests {
274274
use core::fmt;
275275

276276
#[test]
277+
#[cfg(any(feature = "std", feature = "alloc"))]
277278
fn hex_roundtrip() {
278279
let expected = "0123456789abcdef";
279280
let expected_up = "0123456789ABCDEF";
@@ -361,6 +362,7 @@ mod tests {
361362
}
362363

363364
#[test]
365+
#[cfg(any(feature = "std", feature = "alloc"))]
364366
fn hex_error() {
365367
let oddlen = "0123456789abcdef0";
366368
let badchar1 = "Z123456789abcdef";

src/hmac.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,18 @@ impl<'de, T: HashTrait + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
239239

240240
#[cfg(test)]
241241
mod tests {
242-
use sha256;
243-
#[cfg(feature = "serde")] use sha512;
244-
use {Hash, HashEngine, Hmac, HmacEngine};
245-
246-
#[derive(Clone)]
247-
struct Test {
248-
key: Vec<u8>,
249-
input: Vec<u8>,
250-
output: Vec<u8>,
251-
}
252-
253242
#[test]
243+
#[cfg(any(feature = "std", feature = "alloc"))]
254244
fn test() {
245+
use {sha256, HashEngine, HmacEngine, Hash, Hmac};
246+
247+
#[derive(Clone)]
248+
struct Test {
249+
key: Vec<u8>,
250+
input: Vec<u8>,
251+
output: Vec<u8>,
252+
}
253+
255254
let tests = vec![
256255
// Test vectors copied from libsecp256k1
257256
// Sadly the RFC2104 test vectors all use MD5 as their underlying hash function,
@@ -369,6 +368,7 @@ mod tests {
369368
#[test]
370369
fn hmac_sha512_serde() {
371370
use serde_test::{Configure, Token, assert_tokens};
371+
use {sha512, Hash, Hmac};
372372

373373
static HASH_BYTES: [u8; 64] = [
374374
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21,

src/ripemd160.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -459,20 +459,20 @@ impl HashEngine {
459459

460460
#[cfg(test)]
461461
mod tests {
462-
use ripemd160;
463-
use hex::{FromHex, ToHex};
464-
use Hash;
465-
use HashEngine;
466-
467-
#[derive(Clone)]
468-
struct Test {
469-
input: &'static str,
470-
output: Vec<u8>,
471-
output_str: &'static str,
472-
}
473-
474462
#[test]
463+
#[cfg(any(feature = "std", feature = "alloc"))]
475464
fn test() {
465+
use ripemd160;
466+
use {Hash, HashEngine};
467+
use hex::{FromHex, ToHex};
468+
469+
#[derive(Clone)]
470+
struct Test {
471+
input: &'static str,
472+
output: Vec<u8>,
473+
output_str: &'static str,
474+
}
475+
476476
let tests = vec![
477477
// Test messages from FIPS 180-1
478478
Test {
@@ -545,6 +545,7 @@ mod tests {
545545
#[test]
546546
fn ripemd_serde() {
547547
use serde_test::{Configure, Token, assert_tokens};
548+
use {ripemd160, Hash};
548549

549550
static HASH_BYTES: [u8; 20] = [
550551
0x13, 0x20, 0x72, 0xdf,

src/sha1.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,20 @@ impl HashEngine {
197197

198198
#[cfg(test)]
199199
mod tests {
200-
use sha1;
201-
use hex::{FromHex, ToHex};
202-
use Hash;
203-
use HashEngine;
204-
205-
#[derive(Clone)]
206-
struct Test {
207-
input: &'static str,
208-
output: Vec<u8>,
209-
output_str: &'static str,
210-
}
211-
212200
#[test]
201+
#[cfg(any(feature = "std", feature = "alloc"))]
213202
fn test() {
203+
use {sha1, Hash, HashEngine};
204+
use hex::{FromHex, ToHex};
205+
206+
#[derive(Clone)]
207+
struct Test {
208+
input: &'static str,
209+
output: Vec<u8>,
210+
output_str: &'static str,
211+
}
212+
213+
214214
let tests = vec![
215215
// Examples from wikipedia
216216
Test {
@@ -270,6 +270,7 @@ mod tests {
270270
#[test]
271271
fn sha1_serde() {
272272
use serde_test::{Configure, Token, assert_tokens};
273+
use {sha1, Hash};
273274

274275
static HASH_BYTES: [u8; 20] = [
275276
0x13, 0x20, 0x72, 0xdf,

src/sha256.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,20 @@ impl HashEngine {
373373
#[cfg(test)]
374374
mod tests {
375375
use sha256;
376-
use hex::{FromHex, ToHex};
377376
use {Hash, HashEngine};
378377

379-
#[derive(Clone)]
380-
struct Test {
381-
input: &'static str,
382-
output: Vec<u8>,
383-
output_str: &'static str,
384-
}
385-
386378
#[test]
379+
#[cfg(any(feature = "std", feature = "alloc"))]
387380
fn test() {
381+
use hex::{FromHex, ToHex};
382+
383+
#[derive(Clone)]
384+
struct Test {
385+
input: &'static str,
386+
output: Vec<u8>,
387+
output_str: &'static str,
388+
}
389+
388390
let tests = vec![
389391
// Examples from wikipedia
390392
Test {

src/sha256d.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,19 @@ impl HashTrait for Hash {
100100

101101
#[cfg(test)]
102102
mod tests {
103-
use sha256d;
104-
use hex::{FromHex, ToHex};
105-
use Hash;
106-
use HashEngine;
107-
108-
#[derive(Clone)]
109-
struct Test {
110-
input: &'static str,
111-
output: Vec<u8>,
112-
output_str: &'static str,
113-
}
114-
115103
#[test]
104+
#[cfg(any(feature = "std", feature = "alloc"))]
116105
fn test() {
106+
use {sha256d, Hash, HashEngine};
107+
use hex::{FromHex, ToHex};
108+
109+
#[derive(Clone)]
110+
struct Test {
111+
input: &'static str,
112+
output: Vec<u8>,
113+
output_str: &'static str,
114+
}
115+
117116
let tests = vec![
118117
// Test vector copied out of rust-bitcoin
119118
Test {
@@ -150,6 +149,7 @@ mod tests {
150149
#[test]
151150
fn sha256_serde() {
152151
use serde_test::{Configure, Token, assert_tokens};
152+
use {sha256d, Hash};
153153

154154
static HASH_BYTES: [u8; 32] = [
155155
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7,

src/sha256t.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl<'de, T: Tag> ::serde::Deserialize<'de> for Hash<T> {
246246
#[cfg(test)]
247247
mod tests {
248248
use ::{Hash, sha256, sha256t};
249+
#[cfg(any(feature = "std", feature = "alloc"))]
249250
use ::hex::ToHex;
250251

251252
const TEST_MIDSTATE: [u8; 32] = [
@@ -266,11 +267,13 @@ mod tests {
266267
}
267268

268269
/// A hash tagged with `$name`.
270+
#[cfg(any(feature = "std", feature = "alloc"))]
269271
pub type TestHash = sha256t::Hash<TestHashTag>;
270272

271273
sha256t_hash_newtype!(NewTypeHash, NewTypeTag, TEST_MIDSTATE, 64, doc="test hash", true);
272274

273275
#[test]
276+
#[cfg(any(feature = "std", feature = "alloc"))]
274277
fn test_sha256t() {
275278
assert_eq!(
276279
TestHash::hash(&[0]).to_hex(),

src/sha512.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,20 +348,19 @@ impl HashEngine {
348348

349349
#[cfg(test)]
350350
mod tests {
351-
use sha512;
352-
use hex::{FromHex, ToHex};
353-
use Hash;
354-
use HashEngine;
355-
356-
#[derive(Clone)]
357-
struct Test {
358-
input: &'static str,
359-
output: Vec<u8>,
360-
output_str: &'static str,
361-
}
362-
363351
#[test]
352+
#[cfg(any(feature = "std", feature = "alloc"))]
364353
fn test() {
354+
use {sha512, Hash, HashEngine};
355+
use hex::{FromHex, ToHex};
356+
357+
#[derive(Clone)]
358+
struct Test {
359+
input: &'static str,
360+
output: Vec<u8>,
361+
output_str: &'static str,
362+
}
363+
365364
let tests = vec![
366365
// Test vectors computed with `sha512sum`
367366
Test {
@@ -430,6 +429,7 @@ mod tests {
430429
#[test]
431430
fn sha512_serde() {
432431
use serde_test::{Configure, Token, assert_tokens};
432+
use {sha512, Hash};
433433

434434
static HASH_BYTES: [u8; 64] = [
435435
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21,

0 commit comments

Comments
 (0)