chore: split stress tests (part 1)

This commit is contained in:
Friedel Ziegelmayer
2019-05-14 00:37:44 +02:00
committed by GitHub
8 changed files with 943 additions and 1720 deletions

View File

@@ -45,7 +45,7 @@ pub unsafe fn dc_marknoticed_contact(context: &dc_context_t, contact_id: uint32_
);
}
// handle contacts
/// Returns false if addr is an invalid address, otherwise true.
pub unsafe fn dc_may_be_valid_addr(addr: *const libc::c_char) -> bool {
if addr.is_null() {
return false;
@@ -1259,3 +1259,59 @@ pub unsafe fn dc_scaleup_contact_origin(
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dc_may_be_valid_addr() {
unsafe {
assert_eq!(dc_may_be_valid_addr(0 as *const libc::c_char), false);
assert_eq!(
dc_may_be_valid_addr(b"\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"user@domain.tld\x00" as *const u8 as *const libc::c_char),
true
);
assert_eq!(
dc_may_be_valid_addr(b"uuu\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"dd.tt\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"tt.dd@uu\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"u@d\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"u@d.\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"u@d.t\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"u@d.tt\x00" as *const u8 as *const libc::c_char),
true
);
assert_eq!(
dc_may_be_valid_addr(b"u@.tt\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_may_be_valid_addr(b"@d.tt\x00" as *const u8 as *const libc::c_char),
false
);
}
}
}

View File

@@ -1966,3 +1966,82 @@ pub unsafe fn mailimf_find_optional_field(
0 as *mut mailimf_optional_field
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;
#[test]
fn test_mailmime_parse() {
unsafe {
let txt: *const libc::c_char =
b"FieldA: ValueA\nFieldB: ValueB\n\x00" as *const u8 as *const libc::c_char;
let mut mime: *mut mailmime = 0 as *mut mailmime;
let mut dummy: size_t = 0i32 as size_t;
let res = mailmime_parse(txt, strlen(txt), &mut dummy, &mut mime);
assert_eq!(res, MAIL_NO_ERROR as libc::c_int);
assert!(!mime.is_null());
let fields: *mut mailimf_fields = mailmime_find_mailimf_fields(mime);
assert!(!fields.is_null());
let mut of_a: *mut mailimf_optional_field = mailimf_find_optional_field(
fields,
b"fielda\x00" as *const u8 as *const libc::c_char,
);
assert!(!of_a.is_null());
assert!(!(*of_a).fld_value.is_null());
assert_eq!(
CStr::from_ptr((*of_a).fld_name as *const libc::c_char)
.to_str()
.unwrap(),
"FieldA",
);
assert_eq!(
CStr::from_ptr((*of_a).fld_value as *const libc::c_char)
.to_str()
.unwrap(),
"ValueA",
);
of_a = mailimf_find_optional_field(
fields,
b"FIELDA\x00" as *const u8 as *const libc::c_char,
);
assert!(!of_a.is_null());
assert!(!(*of_a).fld_value.is_null());
assert_eq!(
CStr::from_ptr((*of_a).fld_name as *const libc::c_char)
.to_str()
.unwrap(),
"FieldA",
);
assert_eq!(
CStr::from_ptr((*of_a).fld_value as *const libc::c_char)
.to_str()
.unwrap(),
"ValueA",
);
let of_b: *mut mailimf_optional_field = mailimf_find_optional_field(
fields,
b"FieldB\x00" as *const u8 as *const libc::c_char,
);
assert!(!of_b.is_null());
assert!(!(*of_b).fld_value.is_null());
assert_eq!(
CStr::from_ptr((*of_b).fld_value as *const libc::c_char)
.to_str()
.unwrap(),
"ValueB",
);
mailmime_free(mime);
}
}
}

View File

@@ -1,4 +1,4 @@
use crate::constants::Event;
use crate::constants::*;
use crate::dc_chat::*;
use crate::dc_contact::*;
use crate::dc_context::*;
@@ -388,32 +388,32 @@ pub unsafe fn dc_msg_guess_msgtype_from_suffix(
suffix = dc_get_filesuffix_lc(pathNfilename);
if !suffix.is_null() {
if strcmp(suffix, b"mp3\x00" as *const u8 as *const libc::c_char) == 0i32 {
*ret_msgtype = 40i32;
*ret_msgtype = DC_MSG_AUDIO as libc::c_int;
*ret_mime = dc_strdup(b"audio/mpeg\x00" as *const u8 as *const libc::c_char)
} else if strcmp(suffix, b"aac\x00" as *const u8 as *const libc::c_char) == 0i32 {
*ret_msgtype = 40i32;
*ret_msgtype = DC_MSG_AUDIO as libc::c_int;
*ret_mime = dc_strdup(b"audio/aac\x00" as *const u8 as *const libc::c_char)
} else if strcmp(suffix, b"mp4\x00" as *const u8 as *const libc::c_char) == 0i32 {
*ret_msgtype = 50i32;
*ret_msgtype = DC_MSG_VIDEO as libc::c_int;
*ret_mime = dc_strdup(b"video/mp4\x00" as *const u8 as *const libc::c_char)
} else if strcmp(suffix, b"jpg\x00" as *const u8 as *const libc::c_char) == 0i32
|| strcmp(suffix, b"jpeg\x00" as *const u8 as *const libc::c_char) == 0i32
{
*ret_msgtype = 20i32;
*ret_msgtype = DC_MSG_IMAGE as libc::c_int;
*ret_mime = dc_strdup(b"image/jpeg\x00" as *const u8 as *const libc::c_char)
} else if strcmp(suffix, b"png\x00" as *const u8 as *const libc::c_char) == 0i32 {
*ret_msgtype = 20i32;
*ret_msgtype = DC_MSG_IMAGE as libc::c_int;
*ret_mime = dc_strdup(b"image/png\x00" as *const u8 as *const libc::c_char)
} else if strcmp(suffix, b"webp\x00" as *const u8 as *const libc::c_char) == 0i32 {
*ret_msgtype = 20i32;
*ret_msgtype = DC_MSG_IMAGE as libc::c_int;
*ret_mime = dc_strdup(b"image/webp\x00" as *const u8 as *const libc::c_char)
} else if strcmp(suffix, b"gif\x00" as *const u8 as *const libc::c_char) == 0i32 {
*ret_msgtype = 21i32;
*ret_msgtype = DC_MSG_GIF as libc::c_int;
*ret_mime = dc_strdup(b"image/gif\x00" as *const u8 as *const libc::c_char)
} else if strcmp(suffix, b"vcf\x00" as *const u8 as *const libc::c_char) == 0i32
|| strcmp(suffix, b"vcard\x00" as *const u8 as *const libc::c_char) == 0i32
{
*ret_msgtype = 60i32;
*ret_msgtype = DC_MSG_FILE as libc::c_int;
*ret_mime = dc_strdup(b"text/vcard\x00" as *const u8 as *const libc::c_char)
}
}
@@ -1009,7 +1009,14 @@ pub unsafe fn dc_msg_get_summarytext_by_raw(
b"ErrFilename\x00" as *const u8 as *const libc::c_char,
);
value = dc_get_filename(pathNfilename);
label = dc_stock_str(context, if type_0 == 40i32 { 11i32 } else { 12i32 });
label = dc_stock_str(
context,
if type_0 == DC_MSG_AUDIO as libc::c_int {
11i32
} else {
12i32
},
);
prefix = dc_mprintf(
b"%s \xe2\x80\x93 %s\x00" as *const u8 as *const libc::c_char,
label,
@@ -1112,18 +1119,21 @@ pub unsafe fn dc_msg_is_increation(msg: *const dc_msg_t) -> libc::c_int {
return 0i32;
}
(((*msg).type_0 == 20i32
|| (*msg).type_0 == 21i32
|| (*msg).type_0 == 40i32
|| (*msg).type_0 == 41i32
|| (*msg).type_0 == 50i32
|| (*msg).type_0 == 60i32)
(((*msg).type_0 == DC_MSG_IMAGE as libc::c_int
|| (*msg).type_0 == DC_MSG_GIF as libc::c_int
|| (*msg).type_0 == DC_MSG_AUDIO as libc::c_int
|| (*msg).type_0 == DC_MSG_VOICE as libc::c_int
|| (*msg).type_0 == DC_MSG_VIDEO as libc::c_int
|| (*msg).type_0 == DC_MSG_FILE as libc::c_int)
&& (*msg).state == 18i32) as libc::c_int
}
// TODO should return bool /rtn
pub unsafe fn dc_msg_is_setupmessage(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint || (*msg).type_0 != 60i32 {
if msg.is_null()
|| (*msg).magic != 0x11561156i32 as libc::c_uint
|| (*msg).type_0 != DC_MSG_FILE as libc::c_int
{
return 0i32;
}
return if dc_param_get_int((*msg).param, 'S' as i32, 0i32) == 6i32 {
@@ -1616,3 +1626,143 @@ pub unsafe fn dc_update_server_uid(
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;
#[test]
fn test_dc_msg_guess_msgtype_from_suffix() {
unsafe {
let mut type_0: libc::c_int = 0;
let mut mime_0: *mut libc::c_char = 0 as *mut libc::c_char;
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.mp3\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_AUDIO as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"audio/mpeg"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.aac\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_AUDIO as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"audio/aac"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.mp4\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_VIDEO as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"video/mp4"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.jpg\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_IMAGE as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"image/jpeg"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.jpeg\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_IMAGE as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"image/jpeg"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.png\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_IMAGE as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"image/png"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.webp\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_IMAGE as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"image/webp"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.gif\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_GIF as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"image/gif"
);
free(mime_0 as *mut libc::c_void);
dc_msg_guess_msgtype_from_suffix(
b"foo/bar-sth.vcf\x00" as *const u8 as *const libc::c_char,
&mut type_0,
&mut mime_0,
);
assert_eq!(type_0, DC_MSG_FILE as libc::c_int);
assert_eq!(
CStr::from_ptr(mime_0 as *const libc::c_char)
.to_str()
.unwrap(),
"text/vcard"
);
free(mime_0 as *mut libc::c_void);
}
}
}

View File

@@ -73,9 +73,9 @@ pub unsafe fn dc_simplify_simplify(
out
}
/* ******************************************************************************
/**
* Simplify Plain Text
******************************************************************************/
*/
unsafe fn dc_simplify_simplify_plain_text(
mut simplify: *mut dc_simplify_t,
buf_terminated: *const libc::c_char,
@@ -250,9 +250,9 @@ unsafe fn dc_simplify_simplify_plain_text(
ret.buf
}
/* ******************************************************************************
/**
* Tools
******************************************************************************/
*/
unsafe fn is_empty_line(buf: *const libc::c_char) -> bool {
/* force unsigned - otherwise the `> ' '` comparison will fail */
let mut p1: *const libc::c_uchar = buf as *const libc::c_uchar;
@@ -289,3 +289,95 @@ unsafe fn is_plain_quote(buf: *const libc::c_char) -> bool {
false
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;
#[test]
fn test_simplify_trim() {
unsafe {
let simplify: *mut dc_simplify_t = dc_simplify_new();
let html: *const libc::c_char =
b"\r\r\nline1<br>\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char;
let plain: *mut libc::c_char =
dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1, 0);
assert_eq!(
CStr::from_ptr(plain as *const libc::c_char)
.to_str()
.unwrap(),
"line1\nline2",
);
free(plain as *mut libc::c_void);
dc_simplify_unref(simplify);
}
}
#[test]
fn test_simplify_parse_href() {
unsafe {
let simplify: *mut dc_simplify_t = dc_simplify_new();
let html: *const libc::c_char =
b"<a href=url>text</a\x00" as *const u8 as *const libc::c_char;
let plain: *mut libc::c_char =
dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1, 0);
assert_eq!(
CStr::from_ptr(plain as *const libc::c_char)
.to_str()
.unwrap(),
"[text](url)",
);
free(plain as *mut libc::c_void);
dc_simplify_unref(simplify);
}
}
#[test]
fn test_simplify_bold_text() {
unsafe {
let simplify: *mut dc_simplify_t = dc_simplify_new();
let html: *const libc::c_char =
b"<!DOCTYPE name [<!DOCTYPE ...>]><!-- comment -->text <b><?php echo ... ?>bold</b><![CDATA[<>]]>\x00"
as *const u8 as *const libc::c_char;
let plain: *mut libc::c_char =
dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1, 0);
assert_eq!(
CStr::from_ptr(plain as *const libc::c_char)
.to_str()
.unwrap(),
"text *bold*<>",
);
free(plain as *mut libc::c_void);
dc_simplify_unref(simplify);
}
}
#[test]
fn test_simplify_html_encoded() {
unsafe {
let simplify: *mut dc_simplify_t = dc_simplify_new();
let html: *const libc::c_char =
b"&lt;&gt;&quot;&apos;&amp; &auml;&Auml;&ouml;&Ouml;&uuml;&Uuml;&szlig; foo&AElig;&ccedil;&Ccedil; &diams;&noent;&lrm;&rlm;&zwnj;&zwj;\x00"
as *const u8 as *const libc::c_char;
let plain: *mut libc::c_char =
dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1, 0);
assert_eq!(
strcmp(plain,
b"<>\"\'& \xc3\xa4\xc3\x84\xc3\xb6\xc3\x96\xc3\xbc\xc3\x9c\xc3\x9f foo\xc3\x86\xc3\xa7\xc3\x87 \xe2\x99\xa6&noent;\x00"
as *const u8 as *const libc::c_char),
0,
);
free(plain as *mut libc::c_void);
dc_simplify_unref(simplify);
}
}
}

View File

@@ -728,6 +728,7 @@ pub unsafe fn dc_decode_ext_header(to_decode: *const libc::c_char) -> *mut libc:
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;
#[test]
fn test_isalnum() {
@@ -735,4 +736,159 @@ mod tests {
assert_eq!(isalnum('5' as libc::c_int), 1);
assert_eq!(isalnum('Q' as libc::c_int), 1);
}
#[test]
fn test_dc_decode_header_words() {
unsafe {
let mut buf1: *mut libc::c_char = dc_decode_header_words(
b"=?utf-8?B?dGVzdMOkw7bDvC50eHQ=?=\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
strcmp(
buf1,
b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char
),
0
);
free(buf1 as *mut libc::c_void);
buf1 =
dc_decode_header_words(b"just ascii test\x00" as *const u8 as *const libc::c_char);
assert_eq!(CStr::from_ptr(buf1).to_str().unwrap(), "just ascii test");
free(buf1 as *mut libc::c_void);
buf1 = dc_encode_header_words(b"abcdef\x00" as *const u8 as *const libc::c_char);
assert_eq!(CStr::from_ptr(buf1).to_str().unwrap(), "abcdef");
free(buf1 as *mut libc::c_void);
buf1 = dc_encode_header_words(
b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
strncmp(buf1, b"=?utf-8\x00" as *const u8 as *const libc::c_char, 7),
0
);
let buf2: *mut libc::c_char = dc_decode_header_words(buf1);
assert_eq!(
strcmp(
buf2,
b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char
),
0
);
free(buf1 as *mut libc::c_void);
free(buf2 as *mut libc::c_void);
buf1 = dc_decode_header_words(
b"=?ISO-8859-1?Q?attachment=3B=0D=0A_filename=3D?= =?ISO-8859-1?Q?=22test=E4=F6=FC=2Etxt=22=3B=0D=0A_size=3D39?=\x00" as *const u8 as *const libc::c_char
);
assert_eq!(
strcmp(
buf1,
b"attachment;\r\n filename=\"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\";\r\n size=39\x00" as *const u8 as *const libc::c_char,
),
0
);
free(buf1 as *mut libc::c_void);
}
}
#[test]
fn test_dc_encode_ext_header() {
unsafe {
let mut buf1 = dc_encode_ext_header(
b"Bj\xc3\xb6rn Petersen\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
CStr::from_ptr(buf1).to_str().unwrap(),
"utf-8\'\'Bj%C3%B6rn%20Petersen"
);
let buf2 = dc_decode_ext_header(buf1);
assert_eq!(
strcmp(
buf2,
b"Bj\xc3\xb6rn Petersen\x00" as *const u8 as *const libc::c_char,
),
0
);
free(buf1 as *mut libc::c_void);
free(buf2 as *mut libc::c_void);
buf1 = dc_decode_ext_header(
b"iso-8859-1\'en\'%A3%20rates\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
strcmp(
buf1,
b"\xc2\xa3 rates\x00" as *const u8 as *const libc::c_char,
),
0
);
free(buf1 as *mut libc::c_void);
buf1 = dc_decode_ext_header(b"wrong\'format\x00" as *const u8 as *const libc::c_char);
assert_eq!(
strcmp(
buf1,
b"wrong\'format\x00" as *const u8 as *const libc::c_char,
),
0
);
free(buf1 as *mut libc::c_void);
buf1 = dc_decode_ext_header(b"\'\'\x00" as *const u8 as *const libc::c_char);
assert_eq!(
strcmp(buf1, b"\'\'\x00" as *const u8 as *const libc::c_char),
0
);
free(buf1 as *mut libc::c_void);
buf1 = dc_decode_ext_header(b"x\'\'\x00" as *const u8 as *const libc::c_char);
assert_eq!(strcmp(buf1, b"\x00" as *const u8 as *const libc::c_char), 0);
free(buf1 as *mut libc::c_void);
buf1 = dc_decode_ext_header(b"\'\x00" as *const u8 as *const libc::c_char);
assert_eq!(
strcmp(buf1, b"\'\x00" as *const u8 as *const libc::c_char),
0
);
free(buf1 as *mut libc::c_void);
buf1 = dc_decode_ext_header(b"\x00" as *const u8 as *const libc::c_char);
assert_eq!(strcmp(buf1, b"\x00" as *const u8 as *const libc::c_char), 0);
free(buf1 as *mut libc::c_void);
}
}
#[test]
fn test_dc_needs_ext_header() {
unsafe {
assert_eq!(
dc_needs_ext_header(b"Bj\xc3\xb6rn\x00" as *const u8 as *const libc::c_char),
true
);
assert_eq!(
dc_needs_ext_header(b"Bjoern\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_needs_ext_header(b"\x00" as *const u8 as *const libc::c_char),
false
);
assert_eq!(
dc_needs_ext_header(b" \x00" as *const u8 as *const libc::c_char),
true
);
assert_eq!(
dc_needs_ext_header(b"a b\x00" as *const u8 as *const libc::c_char),
true
);
assert_eq!(
dc_needs_ext_header(0 as *const u8 as *const libc::c_char),
false
);
}
}
}

View File

@@ -1704,4 +1704,258 @@ mod tests {
);
}
}
#[test]
fn test_dc_atof() {
unsafe {
let f: libc::c_double = dc_atof(b"1.23\x00" as *const u8 as *const libc::c_char);
assert!(f > 1.22f64);
assert!(f < 1.24f64);
}
}
#[test]
fn test_dc_ftoa() {
unsafe {
let s: *mut libc::c_char = dc_ftoa(1.23f64);
assert!(dc_atof(s) > 1.22f64);
assert!(dc_atof(s) < 1.24f64);
free(s as *mut libc::c_void);
}
}
#[test]
fn test_dc_str_replace() {
unsafe {
let mut str: *mut libc::c_char = strdup(b"aaa\x00" as *const u8 as *const libc::c_char);
let replacements: libc::c_int = dc_str_replace(
&mut str,
b"a\x00" as *const u8 as *const libc::c_char,
b"ab\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"ababab"
);
assert_eq!(replacements, 3);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_str_truncate_1() {
unsafe {
let str: *mut libc::c_char =
strdup(b"this is a little test string\x00" as *const u8 as *const libc::c_char);
dc_truncate_str(str, 16);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"this is a [...]"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_str_truncate_2() {
unsafe {
let str: *mut libc::c_char = strdup(b"1234\x00" as *const u8 as *const libc::c_char);
dc_truncate_str(str, 2);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"1234"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_str_truncate_3() {
unsafe {
let str: *mut libc::c_char = strdup(b"1234567\x00" as *const u8 as *const libc::c_char);
dc_truncate_str(str, 1);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"1[...]"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_str_truncate_4() {
unsafe {
let str: *mut libc::c_char = strdup(b"123456\x00" as *const u8 as *const libc::c_char);
dc_truncate_str(str, 4);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"123456"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_insert_breaks_1() {
unsafe {
let str: *mut libc::c_char = dc_insert_breaks(
b"just1234test\x00" as *const u8 as *const libc::c_char,
4,
b" \x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"just 1234 test"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_insert_breaks_2() {
unsafe {
let str: *mut libc::c_char = dc_insert_breaks(
b"just1234tes\x00" as *const u8 as *const libc::c_char,
4i32,
b"--\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"just--1234--tes"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_insert_breaks_3() {
unsafe {
let str: *mut libc::c_char = dc_insert_breaks(
b"just1234t\x00" as *const u8 as *const libc::c_char,
4i32,
b"\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"just1234t"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_insert_breaks_4() {
unsafe {
let str: *mut libc::c_char = dc_insert_breaks(
b"\x00" as *const u8 as *const libc::c_char,
4i32,
b"---\x00" as *const u8 as *const libc::c_char,
);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
""
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_null_terminate_1() {
unsafe {
let str: *mut libc::c_char =
dc_null_terminate(b"abcxyz\x00" as *const u8 as *const libc::c_char, 3);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"abc"
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_null_terminate_2() {
unsafe {
let str: *mut libc::c_char =
dc_null_terminate(b"abcxyz\x00" as *const u8 as *const libc::c_char, 0);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
""
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_null_terminate_3() {
unsafe {
let str: *mut libc::c_char =
dc_null_terminate(0 as *const u8 as *const libc::c_char, 0);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
""
);
free(str as *mut libc::c_void);
}
}
#[test]
fn test_dc_str_to_clist_1() {
unsafe {
let list: *mut clist = dc_str_to_clist(
0 as *const libc::c_char,
b" \x00" as *const u8 as *const libc::c_char,
);
assert_eq!((*list).count, 0);
clist_free_content(list);
clist_free(list);
}
}
#[test]
fn test_dc_str_to_clist_2() {
unsafe {
let list: *mut clist = dc_str_to_clist(
b"\x00" as *const u8 as *const libc::c_char,
b" \x00" as *const u8 as *const libc::c_char,
);
assert_eq!((*list).count, 1);
clist_free_content(list);
clist_free(list);
}
}
#[test]
fn test_dc_str_to_clist_3() {
unsafe {
let list: *mut clist = dc_str_to_clist(
b" \x00" as *const u8 as *const libc::c_char,
b" \x00" as *const u8 as *const libc::c_char,
);
assert_eq!((*list).count, 2);
clist_free_content(list);
clist_free(list);
}
}
#[test]
fn test_dc_str_to_clist_4() {
unsafe {
let list: *mut clist = dc_str_to_clist(
b"foo bar test\x00" as *const u8 as *const libc::c_char,
b" \x00" as *const u8 as *const libc::c_char,
);
assert_eq!((*list).count, 3);
let str: *mut libc::c_char =
dc_str_from_clist(list, b" \x00" as *const u8 as *const libc::c_char);
assert_eq!(
CStr::from_ptr(str as *const libc::c_char).to_str().unwrap(),
"foo bar test"
);
clist_free_content(list);
clist_free(list);
free(str as *mut libc::c_void);
}
}
}

View File

@@ -76,3 +76,16 @@ pub use libc::atof;
pub unsafe fn atof(nptr: *mut libc::c_char) -> libc::c_double {
libc::strtod(nptr, std::ptr::null_mut())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_atox() {
unsafe {
assert_eq!(atol(b"\x00" as *const u8 as *const libc::c_char), 0);
assert_eq!(atoi(b"\x00" as *const u8 as *const libc::c_char), 0);
}
}
}

File diff suppressed because it is too large Load Diff