Skip to content

Commit a1c832b

Browse files
authored
Merge pull request #10 from eldruin/master
Add CI
2 parents fa08d45 + 0b6c808 commit a1c832b

File tree

4 files changed

+183
-75
lines changed

4 files changed

+183
-75
lines changed

.github/workflows/build.yml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
on:
2+
push:
3+
branches: [ staging, trying, master ]
4+
pull_request:
5+
6+
name: Build
7+
8+
#env:
9+
# RUSTFLAGS: '--deny warnings' # clippy generates warnings at the moment
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
env:
15+
RUSTFLAGS: '--deny warnings'
16+
strategy:
17+
matrix:
18+
rust: [stable]
19+
TARGET:
20+
- x86_64-unknown-linux-gnu
21+
- x86_64-unknown-linux-musl
22+
- arm-unknown-linux-gnueabi # Raspberry Pi 1
23+
- armv7-unknown-linux-gnueabihf # Raspberry Pi 2, 3, etc
24+
# Bare metal
25+
- thumbv6m-none-eabi
26+
- thumbv7em-none-eabi
27+
- thumbv7em-none-eabihf
28+
- thumbv7m-none-eabi
29+
30+
steps:
31+
- uses: actions/checkout@v2
32+
- uses: actions-rs/toolchain@v1
33+
with:
34+
profile: minimal
35+
toolchain: ${{ matrix.rust }}
36+
target: ${{ matrix.TARGET }}
37+
override: true
38+
- uses: actions-rs/cargo@v1
39+
with:
40+
use-cross: true
41+
command: build
42+
args: --target=${{ matrix.TARGET }}
43+
44+
test:
45+
name: Tests
46+
env:
47+
RUSTFLAGS: '--deny warnings'
48+
runs-on: ubuntu-latest
49+
strategy:
50+
matrix:
51+
rust: [stable, beta]
52+
TARGET: [x86_64-unknown-linux-gnu]
53+
54+
steps:
55+
- uses: actions/checkout@v2
56+
- uses: actions-rs/toolchain@v1
57+
with:
58+
profile: minimal
59+
toolchain: ${{ matrix.rust }}
60+
target: ${{ matrix.TARGET }}
61+
override: true
62+
63+
- name: Test
64+
uses: actions-rs/cargo@v1
65+
with:
66+
use-cross: true
67+
command: test
68+
args: --target=${{ matrix.TARGET }}
69+
70+
docs:
71+
runs-on: ubuntu-latest
72+
steps:
73+
- uses: actions/checkout@v2
74+
- uses: actions-rs/toolchain@v1
75+
with:
76+
profile: minimal
77+
toolchain: stable
78+
override: true
79+
components: rustfmt
80+
- uses: actions-rs/cargo@v1
81+
with:
82+
command: doc
83+
84+
fmt:
85+
runs-on: ubuntu-latest
86+
steps:
87+
- uses: actions/checkout@v2
88+
- uses: actions-rs/toolchain@v1
89+
with:
90+
profile: minimal
91+
toolchain: stable
92+
override: true
93+
components: rustfmt
94+
- uses: actions-rs/cargo@v1
95+
with:
96+
command: fmt
97+
args: --all -- --check
98+
99+
clippy:
100+
runs-on: ubuntu-latest
101+
steps:
102+
- uses: actions/checkout@v2
103+
- uses: actions-rs/toolchain@v1
104+
with:
105+
profile: minimal
106+
toolchain: 1.64.0 # clippy is too much of a moving target at the moment
107+
override: true
108+
components: clippy
109+
- uses: actions-rs/clippy-check@v1
110+
with:
111+
token: ${{ secrets.GITHUB_TOKEN }}

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
cc::Build::new()
66
.warnings(true)
77
.extra_warnings(true)
8+
.flag("-std=c99")
89
.file("./src/snprintf.c")
910
.compile("clocal");
1011
}

src/strcpy.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,39 @@ use crate::CChar;
77
/// Rust implementation of C library function `strcpy`. Passing NULL
88
/// (core::ptr::null()) gives undefined behaviour.
99
#[no_mangle]
10-
pub unsafe extern "C" fn strcpy(
11-
dest: *mut CChar,
12-
src: *const CChar,
13-
) -> *const CChar {
14-
let mut i = 0;
15-
loop {
16-
*dest.offset(i) = *src.offset(i);
17-
let c = *dest.offset(i);
18-
if c == 0 {
19-
break;
20-
}
21-
i += 1;
22-
}
23-
dest
10+
pub unsafe extern "C" fn strcpy(dest: *mut CChar, src: *const CChar) -> *const CChar {
11+
let mut i = 0;
12+
loop {
13+
*dest.offset(i) = *src.offset(i);
14+
let c = *dest.offset(i);
15+
if c == 0 {
16+
break;
17+
}
18+
i += 1;
19+
}
20+
dest
2421
}
2522

2623
#[cfg(test)]
2724
mod test {
28-
use super::*;
25+
use super::*;
2926

30-
#[test]
31-
fn short() {
32-
let src = b"hi\0";
33-
let mut dest = *b"abcdef"; // no null terminator
34-
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
35-
assert_eq!(
36-
unsafe { core::slice::from_raw_parts(result, 5) },
37-
*b"hi\0de"
38-
);
39-
}
27+
#[test]
28+
fn short() {
29+
let src = b"hi\0";
30+
let mut dest = *b"abcdef"; // no null terminator
31+
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
32+
assert_eq!(
33+
unsafe { core::slice::from_raw_parts(result, 5) },
34+
*b"hi\0de"
35+
);
36+
}
4037

41-
#[test]
42-
fn two() {
43-
let src = b"hi\0";
44-
let mut dest = [0u8; 2]; // no space for null terminator
45-
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
46-
assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi");
47-
}
38+
#[test]
39+
fn two() {
40+
let src = b"hi\0";
41+
let mut dest = [0u8; 2]; // no space for null terminator
42+
let result = unsafe { strcpy(dest.as_mut_ptr(), src.as_ptr()) };
43+
assert_eq!(unsafe { core::slice::from_raw_parts(result, 2) }, b"hi");
44+
}
4845
}

src/strtoul.rs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Copyright (c) Jonathan 'theJPster' Pallant 2019
44
//! Licensed under the Blue Oak Model Licence 1.0.0
55
6-
use crate::{CChar, CULong, CStringIter};
6+
use crate::{CChar, CStringIter, CULong};
77

88
/// Rust implementation of C library function `strtoul`.
99
///
@@ -13,56 +13,55 @@ use crate::{CChar, CULong, CStringIter};
1313
/// function returns zero.
1414
#[no_mangle]
1515
pub unsafe extern "C" fn strtoul(s: *const CChar) -> CULong {
16-
let mut result: CULong = 0;
17-
for c in CStringIter::new(s) {
18-
if c >= b'0' && c <= b'9' {
19-
result *= 10;
20-
result += (c - b'0') as CULong;
21-
} else {
22-
break;
23-
}
24-
}
25-
result
16+
let mut result: CULong = 0;
17+
for c in CStringIter::new(s) {
18+
if c >= b'0' && c <= b'9' {
19+
result *= 10;
20+
result += (c - b'0') as CULong;
21+
} else {
22+
break;
23+
}
24+
}
25+
result
2626
}
2727

2828
#[cfg(test)]
2929
mod test {
30-
use super::strtoul;
30+
use super::strtoul;
3131

32-
#[test]
33-
fn empty() {
34-
let result = unsafe { strtoul(b"\0".as_ptr()) };
35-
assert_eq!(result, 0);
36-
}
32+
#[test]
33+
fn empty() {
34+
let result = unsafe { strtoul(b"\0".as_ptr()) };
35+
assert_eq!(result, 0);
36+
}
3737

38-
#[test]
39-
fn non_digit() {
40-
let result = unsafe { strtoul(b"1234x\0".as_ptr()) };
41-
assert_eq!(result, 1234);
42-
}
38+
#[test]
39+
fn non_digit() {
40+
let result = unsafe { strtoul(b"1234x\0".as_ptr()) };
41+
assert_eq!(result, 1234);
42+
}
4343

44-
#[test]
45-
fn bad_number() {
46-
let result = unsafe { strtoul(b"x\0".as_ptr()) };
47-
assert_eq!(result, 0);
48-
}
44+
#[test]
45+
fn bad_number() {
46+
let result = unsafe { strtoul(b"x\0".as_ptr()) };
47+
assert_eq!(result, 0);
48+
}
4949

50-
#[test]
51-
fn one() {
52-
let result = unsafe { strtoul(b"1\0".as_ptr()) };
53-
assert_eq!(result, 1);
54-
}
50+
#[test]
51+
fn one() {
52+
let result = unsafe { strtoul(b"1\0".as_ptr()) };
53+
assert_eq!(result, 1);
54+
}
5555

56-
#[test]
57-
fn hundredish() {
58-
let result = unsafe { strtoul(b"123\0".as_ptr()) };
59-
assert_eq!(result, 123);
60-
}
61-
62-
#[test]
63-
fn big_long() {
64-
let result = unsafe { strtoul(b"2147483647\0".as_ptr()) };
65-
assert_eq!(result, 2147483647);
66-
}
56+
#[test]
57+
fn hundredish() {
58+
let result = unsafe { strtoul(b"123\0".as_ptr()) };
59+
assert_eq!(result, 123);
60+
}
6761

62+
#[test]
63+
fn big_long() {
64+
let result = unsafe { strtoul(b"2147483647\0".as_ptr()) };
65+
assert_eq!(result, 2147483647);
66+
}
6867
}

0 commit comments

Comments
 (0)