Skip to content

Commit

Permalink
Move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RoDmitry committed Dec 31, 2023
1 parent 921c641 commit 7bb2f47
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 50 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name = "atoi_simd"
version = "0.15.5"
authors = ["Dmitry Rodionov <gh@rdmtr.com>"]
description = "Fast `&[u8]` to integer parser"
repository = "https://github.com/RoDmitry/atoi_simd"
documentation = "https://docs.rs/atoi_simd/"
keywords = ["atoi", "parseint", "parse", "u8", "simd"]
repository = "https://github.com/RoDmitry/atoi_simd"
categories = ["parsing", "no-std"]
exclude = [".github/*"]
keywords = ["atoi", "parseint", "parse", "u8", "simd"]
exclude = [".github/*", "tests/*"]
readme = "README.md"
license = "MIT OR Apache-2.0"
edition = "2018"
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ mod short;
),
))]
mod simd;
#[cfg(test)]
mod test;

pub use crate::error::AtoiSimdError;
use crate::linker::{Parse, ParseNeg};
pub use crate::linker::{Parse, ParseNeg};

/// Parses slice of digits, and checks first '-' char for signed integers.
#[inline]
Expand Down
2 changes: 2 additions & 0 deletions src/linker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ pub trait ParsePos: Sized {
fn atoi_simd_parse_until_invalid_pos(s: &[u8]) -> Result<(Self, usize), AtoiSimdError>;
}

/// Note: all functions are `#[inline(always)]`
pub trait ParseNeg: Sized {
fn atoi_simd_parse_neg(s: &[u8]) -> Result<Self, AtoiSimdError>;
fn atoi_simd_parse_until_invalid_neg(s: &[u8]) -> Result<(Self, usize), AtoiSimdError>;
}

/// Note: all functions are `#[inline(always)]`
pub trait Parse: ParsePos {
#[inline(always)]
fn atoi_simd_parse(s: &[u8]) -> Result<Self, AtoiSimdError> {
Expand Down
44 changes: 44 additions & 0 deletions tests/roundtrip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use atoi_simd::*;
use numtoa::NumToA;

#[test]
fn roundtrip_all_u8() {
let mut buf = [0; 64];
for i in u8::MIN..=u8::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<u8>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}

#[test]
fn roundtrip_all_i8() {
let mut buf = [0; 64];
for i in i8::MIN..=i8::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<i8>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}

#[test]
#[cfg_attr(miri, ignore)] // too slow in miri
fn roundtrip_all_u16() {
let mut buf = [0; 64];
for i in u16::MIN..=u16::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<u16>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}

#[test]
#[cfg_attr(miri, ignore)] // too slow in miri
fn roundtrip_all_i16() {
let mut buf = [0; 64];
for i in i16::MIN..=i16::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<i16>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}
45 changes: 1 addition & 44 deletions src/test.rs → tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,11 @@
use super::*;
use ::core::cmp::PartialEq;
use ::core::fmt::Debug;
use ::core::str::FromStr;
use arrayvec::ArrayString;
use numtoa::NumToA;
use atoi_simd::*;

const INVALID_CHARS: [char; 6] = ['/', ':', '\0', '\x7f', '!', 'a'];

#[test]
fn roundtrip_all_u8() {
let mut buf = [0; 64];
for i in u8::MIN..=u8::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<u8>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}

#[test]
fn roundtrip_all_i8() {
let mut buf = [0; 64];
for i in i8::MIN..=i8::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<i8>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}

#[test]
#[cfg_attr(miri, ignore)] // too slow in miri
fn roundtrip_all_u16() {
let mut buf = [0; 64];
for i in u16::MIN..=u16::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<u16>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}

#[test]
#[cfg_attr(miri, ignore)] // too slow in miri
fn roundtrip_all_i16() {
let mut buf = [0; 64];
for i in i16::MIN..=i16::MAX {
let input = i.numtoa(10, &mut buf);
let parsed = crate::parse::<i16>(input).expect("Failed to parse valid input!");
assert_eq!(i, parsed);
}
}

fn test_each_position<T: Copy>(s: &str, func: fn(&[u8]) -> Result<T, AtoiSimdError>) {
let mut s_new = ArrayString::<40>::new();
for j in 0..=s.len() {
Expand Down

0 comments on commit 7bb2f47

Please sign in to comment.