From 6ced871d9a59cc1785094a1cff60d271aa275983 Mon Sep 17 00:00:00 2001 From: MggMuggins <mggmugginsmc@gmail.com> Date: Thu, 8 Mar 2018 18:25:11 -0600 Subject: [PATCH] ctype: tests and reorganize Wrote tests for the functions implemented in 40558b2 and maybe made that test a little more comprehensible. Also made sure that fmt.sh and test.sh were being executed by bash all the time (compatibility with other shells). --- fmt.sh | 1 + src/ctype/src/lib.rs | 16 ++++++++-------- src/platform/src/linux/mod.rs | 2 +- src/platform/src/redox/mod.rs | 2 +- src/stdio/src/lib.rs | 2 +- test.sh | 1 + tests/ctype.c | 23 +++++++++++++++++------ 7 files changed, 30 insertions(+), 17 deletions(-) diff --git a/fmt.sh b/fmt.sh index 8e925fc02..4e2cf0508 100755 --- a/fmt.sh +++ b/fmt.sh @@ -1,3 +1,4 @@ +#!/bin/bash ARGS=() for crate in relibc $(find src -name Cargo.toml | cut -d '/' -f2 | grep -v template) diff --git a/src/ctype/src/lib.rs b/src/ctype/src/lib.rs index 19ee5e1a2..bcbc443f5 100644 --- a/src/ctype/src/lib.rs +++ b/src/ctype/src/lib.rs @@ -21,6 +21,11 @@ pub extern "C" fn isascii(c: c_int) -> c_int { (!(c & !0x7f)) as c_int } +#[no_mangle] +pub extern "C" fn isblank(c: c_int) -> c_int { + (c == ' ' as c_int || c == '\t' as c_int) as c_int +} + #[no_mangle] pub extern "C" fn iscntrl(c: c_int) -> c_int { ((c as c_uint) < 0x20 || c == 0x7f) as c_int @@ -48,7 +53,7 @@ pub extern "C" fn isprint(c: c_int) -> c_int { #[no_mangle] pub extern "C" fn ispunct(c: c_int) -> c_int { - (isgraph(c) == 0 && !isalnum(c) == 0) as c_int + (isgraph(c) != 0 && !isalnum(c) != 0) as c_int } #[no_mangle] @@ -63,17 +68,12 @@ pub extern "C" fn isupper(c: c_int) -> c_int { #[no_mangle] pub extern "C" fn isxdigit(c: c_int) -> c_int { - (isdigit(c) == 0 || ((c as c_int) | 32) - ('a' as c_int) < 6) as c_int -} - -#[no_mangle] -pub extern "C" fn isblank(c: c_int) -> c_int { - (c == ' ' as c_int || c == '\t' as c_int) as c_int + (isdigit(c) != 0 || ((c as c_int) | 32) - ('a' as c_int) < 6) as c_int } #[no_mangle] /// The comment in musl: -/// `/* nonsense function that should NEVER be used! */` +/// "nonsense function that should NEVER be used!" pub extern "C" fn toascii(c: c_int) -> c_int { c & 0x7f } diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index 66fafabbb..2d2034f56 100644 --- a/src/platform/src/linux/mod.rs +++ b/src/platform/src/linux/mod.rs @@ -63,7 +63,7 @@ pub fn fchdir(fildes: c_int) -> c_int { } pub fn fork() -> pid_t { - e(unsafe {syscall!(FORK) }) as pid_t + e(unsafe { syscall!(FORK) }) as pid_t } pub fn fsync(fildes: c_int) -> c_int { diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 5a18ed234..bf33529af 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -64,7 +64,7 @@ pub fn fchdir(fd: c_int) -> c_int { } pub fn fork() -> pid_t { - e(unsafe { syscall::clone(0) }) as pid_t + e(unsafe { syscall::clone(0) }) as pid_t } pub fn fsync(fd: c_int) -> c_int { diff --git a/src/stdio/src/lib.rs b/src/stdio/src/lib.rs index 83d444631..689642c54 100644 --- a/src/stdio/src/lib.rs +++ b/src/stdio/src/lib.rs @@ -2,9 +2,9 @@ #![no_std] +extern crate errno; extern crate platform; extern crate va_list as vl; -extern crate errno; use core::str; use core::fmt::Write; diff --git a/test.sh b/test.sh index cb7f9eba9..27f033bb6 100755 --- a/test.sh +++ b/test.sh @@ -1,3 +1,4 @@ +#!/bin/bash set -ex cargo build diff --git a/tests/ctype.c b/tests/ctype.c index a8e9dc10e..272a49d35 100644 --- a/tests/ctype.c +++ b/tests/ctype.c @@ -6,17 +6,28 @@ struct test_case { int isalnum; int isalpha; int isascii; + int isblank; + int iscntrl; int isdigit; + int isgraph; int islower; + int isprint; + int ispunct; int isspace; int isupper; + int isxdigit; } test_cases[] = { - { 'A', 1, 1, 1, 0, 0, 0, 1}, - { 'z', 1, 1, 1, 0, 1, 0, 0}, - { ' ', 0, 0, 1, 0, 0, 1, 0}, - { '1', 1, 0, 1, 1, 0, 0, 0}, - { '9', 1, 0, 1, 1, 0, 0, 0}, - {0x80, 0, 0, 0, 0, 0, 0, 0} + // a a a b c d g l p p s u x + // l l s l n i r o r u p p d + // n p c a t g a w i n a p i + // u h i n r i p e n c c e g + // m a i k l t h r t t e r i + { 'A', 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1}, + { 'z', 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0}, + { ' ', 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0}, + { '1', 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1}, + { '9', 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1}, + {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; size_t num_test_cases = sizeof(test_cases)/sizeof(struct test_case); -- GitLab