mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
126 lines
3 KiB
Rust
126 lines
3 KiB
Rust
#[macro_use]
|
|
extern crate pretty_assertions;
|
|
// #[macro_use]
|
|
// extern crate indoc;
|
|
extern crate quickcheck;
|
|
extern crate roc_std;
|
|
|
|
use core::ffi::c_void;
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void {
|
|
libc::malloc(size)
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn roc_realloc(
|
|
c_ptr: *mut c_void,
|
|
new_size: usize,
|
|
_old_size: usize,
|
|
_alignment: u32,
|
|
) -> *mut c_void {
|
|
libc::realloc(c_ptr, new_size)
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) {
|
|
libc::free(c_ptr)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test_roc_std {
|
|
use roc_std::RocResult;
|
|
use roc_std::RocStr;
|
|
|
|
fn roc_str_byte_representation(string: &RocStr) -> [u8; RocStr::SIZE] {
|
|
unsafe { core::mem::transmute_copy(string) }
|
|
}
|
|
|
|
#[test]
|
|
fn roc_str_empty() {
|
|
let actual = roc_str_byte_representation(&RocStr::empty());
|
|
|
|
let mut expected = [0u8; RocStr::SIZE];
|
|
expected[RocStr::SIZE - 1] = RocStr::MASK;
|
|
|
|
assert_eq!(actual, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn roc_str_single_char() {
|
|
let actual = roc_str_byte_representation(&RocStr::from("a"));
|
|
|
|
let mut expected = [0u8; RocStr::SIZE];
|
|
expected[0] = b'a';
|
|
expected[RocStr::SIZE - 1] = RocStr::MASK | 1;
|
|
|
|
assert_eq!(actual, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn roc_str_max_small_string() {
|
|
let s = str::repeat("a", RocStr::SIZE - 1);
|
|
let actual = roc_str_byte_representation(&RocStr::from(s.as_str()));
|
|
|
|
let mut expected = [0u8; RocStr::SIZE];
|
|
expected[..RocStr::SIZE - 1].copy_from_slice(s.as_bytes());
|
|
expected[RocStr::SIZE - 1] = RocStr::MASK | s.len() as u8;
|
|
|
|
assert_eq!(actual, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_string_from_str() {
|
|
let a = RocStr::from("");
|
|
let b = RocStr::empty();
|
|
|
|
assert_eq!(a, b);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_string_length() {
|
|
let string = RocStr::from("");
|
|
|
|
assert_eq!(string.len(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_string_capacity() {
|
|
let string = RocStr::from("");
|
|
|
|
assert_eq!(string.capacity(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn roc_result_to_rust_result() {
|
|
let greeting = "Hello, World!";
|
|
let roc_result: RocResult<String, ()> = RocResult::ok(greeting.into());
|
|
|
|
match roc_result.into() {
|
|
Ok(answer) => {
|
|
assert_eq!(answer.as_str(), greeting);
|
|
}
|
|
Err(()) => {
|
|
panic!("Received an Err when Ok was expected.")
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn roc_result_is_ok() {
|
|
let greeting = "Hello, World!";
|
|
let roc_result: RocResult<String, ()> = RocResult::ok(greeting.into());
|
|
|
|
assert!(roc_result.is_ok());
|
|
assert!(!roc_result.is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn roc_result_is_err() {
|
|
let greeting = "Hello, World!";
|
|
let roc_result: RocResult<(), String> = RocResult::err(greeting.into());
|
|
|
|
assert!(!roc_result.is_ok());
|
|
assert!(roc_result.is_err());
|
|
}
|
|
}
|