mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
add tests for RocStr clone
This commit is contained in:
parent
0abc1cce15
commit
6749606450
2 changed files with 24 additions and 4 deletions
|
@ -526,4 +526,16 @@ mod gen_str {
|
|||
assert_evals_to!(r#""a" != "b""#, true, bool);
|
||||
assert_evals_to!(r#""a" == "b""#, false, bool);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn str_clone() {
|
||||
use roc_std::RocStr;
|
||||
let long = RocStr::from_slice("loremipsumdolarsitamet".as_bytes());
|
||||
let short = RocStr::from_slice("x".as_bytes());
|
||||
let empty = RocStr::from_slice("".as_bytes());
|
||||
|
||||
debug_assert_eq!(long.clone(), long);
|
||||
debug_assert_eq!(short.clone(), short);
|
||||
debug_assert_eq!(empty.clone(), empty);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -443,18 +443,26 @@ impl Eq for RocStr {}
|
|||
|
||||
impl Clone for RocStr {
|
||||
fn clone(&self) -> Self {
|
||||
if self.is_small_str() {
|
||||
if self.is_small_str() || self.is_empty() {
|
||||
Self {
|
||||
elements: self.elements,
|
||||
length: self.length,
|
||||
}
|
||||
} else {
|
||||
let capacity_size = core::mem::size_of::<usize>();
|
||||
let copy_length = self.length + capacity_size;
|
||||
let elements = unsafe {
|
||||
let raw = libc::malloc(self.length);
|
||||
let raw = libc::malloc(copy_length);
|
||||
|
||||
libc::memcpy(raw, self.elements as *mut libc::c_void, self.length);
|
||||
libc::memcpy(
|
||||
raw,
|
||||
self.elements.offset(-(capacity_size as isize)) as *mut libc::c_void,
|
||||
copy_length,
|
||||
);
|
||||
|
||||
raw as *mut u8
|
||||
*(raw as *mut usize) = self.length;
|
||||
|
||||
(raw as *mut u8).add(capacity_size)
|
||||
};
|
||||
|
||||
Self {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue