Skip to content

Commit

Permalink
Strverscmp fix (#104)
Browse files Browse the repository at this point in the history
* fix strverscmp for comparing "00" and "000"

* add test for strverscmp
  • Loading branch information
KGrewal1 authored Dec 23, 2023
1 parent 49cafa8 commit 55fe7f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.cargo.features": [
"todo"
]
}
25 changes: 24 additions & 1 deletion c-scape/src/mem/ntbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ unsafe extern "C" fn strverscmp(mut s1: *const c_char, mut s2: *const c_char) ->
s2 = s2.add(1);
}

*s1 as c_uchar as c_int - *s2 as c_uchar as c_int
state.exit(s1, s2)
}

#[no_mangle]
Expand Down Expand Up @@ -628,3 +628,26 @@ unsafe extern "C" fn strlcat(dst: *mut c_char, src: *const c_char, limit: size_t

strlcpy(dst.add(len), src, limit - len) + len
}

#[cfg(test)]
mod tests {

// use candle_core::test_utils::{to_vec0_round, to_vec2_round};
use super::*;
#[test]
fn strverscmp_test() {
extern "C" {
fn strverscmp(a: *const libc::c_char, b: *const libc::c_char) -> libc::c_int;
}
unsafe {
assert!(strverscmp("000\0".as_ptr().cast(), "00\0".as_ptr().cast()) < 0);
assert!(strverscmp("00\0".as_ptr().cast(), "01\0".as_ptr().cast()) < 0);
assert!(strverscmp("01\0".as_ptr().cast(), "010\0".as_ptr().cast()) < 0);
assert!(strverscmp("010\0".as_ptr().cast(), "09\0".as_ptr().cast()) < 0);
assert!(strverscmp("09\0".as_ptr().cast(), "0\0".as_ptr().cast()) < 0);
assert!(strverscmp("0\0".as_ptr().cast(), "1\0".as_ptr().cast()) < 0);
assert!(strverscmp("1\0".as_ptr().cast(), "9\0".as_ptr().cast()) < 0);
assert!(strverscmp("9\0".as_ptr().cast(), "10\0".as_ptr().cast()) < 0);
}
}
}

0 comments on commit 55fe7f4

Please sign in to comment.