Skip to content

Commit

Permalink
Add no-std test for macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Oct 31, 2020
1 parent a89028b commit 21eb699
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Build Status][travis_badge]][travis]
[![Build Status][appveyor_badge]][appveyor]
[![Coverage Status][coveralls_badge]][coveralls]
[![dependency status][dependency_badge]][dependency]
[![Dependency Status][dependency_badge]][dependency]

This repository provides a Rust [library] and a [binary] providing efficient
common and custom data-encodings.
Expand Down
3 changes: 2 additions & 1 deletion nostd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ license = "MIT"
publish = false

[features]
alloc = ["data-encoding/alloc"]
alloc = ["data-encoding/alloc", "data-encoding-macro"]

[dependencies]
data-encoding = { path = "../lib", default-features = false }
data-encoding-macro = { path = "../lib/macro", optional = true }
libc = { version = "0.2", default-features = false }

[profile.release]
Expand Down
25 changes: 21 additions & 4 deletions nostd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
#![no_std]
#![no_main]

extern crate data_encoding;
extern crate libc;

use core::fmt::Write;
use data_encoding::{Encoding, BASE32, BASE64, BASE64_NOPAD, HEXLOWER_PERMISSIVE};
use data_encoding::Encoding;

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
Expand Down Expand Up @@ -75,13 +72,33 @@ fn test(encoding: &Encoding, input: &[u8], output: &str, buffer: &mut [u8]) {
test_decode(encoding, output, buffer, input);
}

#[cfg(feature = "alloc")]
fn test_macro() {
// TODO(ia0): We shouldn't need to import decode_slice. Might be fixed by #36.
use data_encoding_macro::{
base64, decode_slice, internal_decode_slice, internal_new_encoding, new_encoding,
};

const FOOBAR: &'static [u8] = &base64!("Zm9vYmFy");
const LETTER8: Encoding = new_encoding! {
symbols: "ABCDEFGH",
};

assert_eq!(FOOBAR, b"foobar");
test(&LETTER8, &[0], "AAA", &mut [0; 3]);
test(&LETTER8, b"foobar", "DBEGHFFHDAEGAFGC", &mut [0; 16]);
}

#[no_mangle]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
use data_encoding::{BASE32, BASE64, BASE64_NOPAD, HEXLOWER_PERMISSIVE};
test(&BASE32, b"hello", "NBSWY3DP", &mut [0; 8]);
test(&BASE64, b"hello", "aGVsbG8=", &mut [0; 8]);
test(&BASE64_NOPAD, b"hello", "aGVsbG8", &mut [0; 8]);
test(&HEXLOWER_PERMISSIVE, b"hello", "68656c6c6f", &mut [0; 10]);
test_decode(&HEXLOWER_PERMISSIVE, "68656C6C6F", &mut [0; 5], b"hello");
#[cfg(feature = "alloc")]
test_macro();
let _ = writeln!(Fd(1), "All tests passed.");
0
}

0 comments on commit 21eb699

Please sign in to comment.