Skip to content

Commit

Permalink
Merge pull request #10 from eldruin/master
Browse files Browse the repository at this point in the history
Add CI
  • Loading branch information
thejpster authored Oct 18, 2022
2 parents fa08d45 + 0b6c808 commit a1c832b
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 75 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
on:
push:
branches: [ staging, trying, master ]
pull_request:

name: Build

#env:
# RUSTFLAGS: '--deny warnings' # clippy generates warnings at the moment

jobs:
build:
runs-on: ubuntu-latest
env:
RUSTFLAGS: '--deny warnings'
strategy:
matrix:
rust: [stable]
TARGET:
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
- arm-unknown-linux-gnueabi # Raspberry Pi 1
- armv7-unknown-linux-gnueabihf # Raspberry Pi 2, 3, etc
# Bare metal
- thumbv6m-none-eabi
- thumbv7em-none-eabi
- thumbv7em-none-eabihf
- thumbv7m-none-eabi

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.TARGET }}
override: true
- uses: actions-rs/cargo@v1
with:
use-cross: true
command: build
args: --target=${{ matrix.TARGET }}

test:
name: Tests
env:
RUSTFLAGS: '--deny warnings'
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, beta]
TARGET: [x86_64-unknown-linux-gnu]

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.TARGET }}
override: true

- name: Test
uses: actions-rs/cargo@v1
with:
use-cross: true
command: test
args: --target=${{ matrix.TARGET }}

docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt
- uses: actions-rs/cargo@v1
with:
command: doc

fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.64.0 # clippy is too much of a moving target at the moment
override: true
components: clippy
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fn main() {
cc::Build::new()
.warnings(true)
.extra_warnings(true)
.flag("-std=c99")
.file("./src/snprintf.c")
.compile("clocal");
}
61 changes: 29 additions & 32 deletions src/strcpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,39 @@ use crate::CChar;
/// Rust implementation of C library function `strcpy`. Passing NULL
/// (core::ptr::null()) gives undefined behaviour.
#[no_mangle]
pub unsafe extern "C" fn strcpy(
dest: *mut CChar,
src: *const CChar,
) -> *const CChar {
let mut i = 0;
loop {
*dest.offset(i) = *src.offset(i);
let c = *dest.offset(i);
if c == 0 {
break;
}
i += 1;
}
dest
pub unsafe extern "C" fn strcpy(dest: *mut CChar, src: *const CChar) -> *const CChar {
let mut i = 0;
loop {
*dest.offset(i) = *src.offset(i);
let c = *dest.offset(i);
if c == 0 {
break;
}
i += 1;
}
dest
}

#[cfg(test)]
mod test {
use super::*;
use super::*;

#[test]
fn short() {
let src = b"hi\0";
let mut dest = *b"abcdef"; // no null terminator
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
assert_eq!(
unsafe { core::slice::from_raw_parts(result, 5) },
*b"hi\0de"
);
}
#[test]
fn short() {
let src = b"hi\0";
let mut dest = *b"abcdef"; // no null terminator
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
assert_eq!(
unsafe { core::slice::from_raw_parts(result, 5) },
*b"hi\0de"
);
}

#[test]
fn two() {
let src = b"hi\0";
let mut dest = [0u8; 2]; // no space for null terminator
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi");
}
#[test]
fn two() {
let src = b"hi\0";
let mut dest = [0u8; 2]; // no space for null terminator
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi");
}
}
85 changes: 42 additions & 43 deletions src/strtoul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Copyright (c) Jonathan 'theJPster' Pallant 2019
//! Licensed under the Blue Oak Model Licence 1.0.0
use crate::{CChar, CULong, CStringIter};
use crate::{CChar, CStringIter, CULong};

/// Rust implementation of C library function `strtoul`.
///
Expand All @@ -13,56 +13,55 @@ use crate::{CChar, CULong, CStringIter};
/// function returns zero.
#[no_mangle]
pub unsafe extern "C" fn strtoul(s: *const CChar) -> CULong {
let mut result: CULong = 0;
for c in CStringIter::new(s) {
if c >= b'0' && c <= b'9' {
result *= 10;
result += (c - b'0') as CULong;
} else {
break;
}
}
result
let mut result: CULong = 0;
for c in CStringIter::new(s) {
if c >= b'0' && c <= b'9' {
result *= 10;
result += (c - b'0') as CULong;
} else {
break;
}
}
result
}

#[cfg(test)]
mod test {
use super::strtoul;
use super::strtoul;

#[test]
fn empty() {
let result = unsafe { strtoul(b"\0".as_ptr()) };
assert_eq!(result, 0);
}
#[test]
fn empty() {
let result = unsafe { strtoul(b"\0".as_ptr()) };
assert_eq!(result, 0);
}

#[test]
fn non_digit() {
let result = unsafe { strtoul(b"1234x\0".as_ptr()) };
assert_eq!(result, 1234);
}
#[test]
fn non_digit() {
let result = unsafe { strtoul(b"1234x\0".as_ptr()) };
assert_eq!(result, 1234);
}

#[test]
fn bad_number() {
let result = unsafe { strtoul(b"x\0".as_ptr()) };
assert_eq!(result, 0);
}
#[test]
fn bad_number() {
let result = unsafe { strtoul(b"x\0".as_ptr()) };
assert_eq!(result, 0);
}

#[test]
fn one() {
let result = unsafe { strtoul(b"1\0".as_ptr()) };
assert_eq!(result, 1);
}
#[test]
fn one() {
let result = unsafe { strtoul(b"1\0".as_ptr()) };
assert_eq!(result, 1);
}

#[test]
fn hundredish() {
let result = unsafe { strtoul(b"123\0".as_ptr()) };
assert_eq!(result, 123);
}

#[test]
fn big_long() {
let result = unsafe { strtoul(b"2147483647\0".as_ptr()) };
assert_eq!(result, 2147483647);
}
#[test]
fn hundredish() {
let result = unsafe { strtoul(b"123\0".as_ptr()) };
assert_eq!(result, 123);
}

#[test]
fn big_long() {
let result = unsafe { strtoul(b"2147483647\0".as_ptr()) };
assert_eq!(result, 2147483647);
}
}

0 comments on commit a1c832b

Please sign in to comment.