From a051556990de7c80765b87ed42d62d4521318038 Mon Sep 17 00:00:00 2001 From: Jos van den Oever Date: Tue, 15 May 2018 17:08:58 +0200 Subject: [PATCH] Add more tests --- demo/src/Bindings.cpp | 14 +- src/cpp.cpp | 17 +- src/rust.cpp | 9 +- tests/CMakeLists.txt | 1 + tests/rust_list_types/Cargo.toml | 13 + tests/rust_list_types/src/implementation.rs | 151 ++++ tests/rust_list_types/src/interface.rs | 492 +++++++++++++ tests/rust_list_types/src/lib.rs | 4 + tests/rust_object_types/src/implementation.rs | 141 +++- tests/rust_object_types/src/interface.rs | 191 ++++- tests/test_list_rust.cpp | 2 +- tests/test_list_types.cpp | 274 +++++++ tests/test_list_types.json | 78 ++ tests/test_list_types_rust.cpp | 674 ++++++++++++++++++ tests/test_list_types_rust.h | 78 ++ tests/test_object_types.cpp | 135 +++- tests/test_object_types.json | 32 +- tests/test_object_types_rust.cpp | 144 +++- tests/test_object_types_rust.h | 44 +- tests/test_tree_rust.cpp | 2 +- 20 files changed, 2370 insertions(+), 126 deletions(-) create mode 100644 tests/rust_list_types/Cargo.toml create mode 100644 tests/rust_list_types/src/implementation.rs create mode 100644 tests/rust_list_types/src/interface.rs create mode 100644 tests/rust_list_types/src/lib.rs create mode 100644 tests/test_list_types.cpp create mode 100644 tests/test_list_types.json create mode 100644 tests/test_list_types_rust.cpp create mode 100644 tests/test_list_types_rust.h diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 4585441..10ee4f8 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -36,8 +36,12 @@ namespace { typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes); void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) { - v->truncate(0); - v->append(bytes, nbytes); + if (v->isNull() && nbytes == 0) { + *v = QByteArray(bytes, nbytes); + } else { + v->truncate(0); + v->append(bytes, nbytes); + } } struct qmodelindex_t { @@ -805,10 +809,10 @@ QVariant TimeSeries::cos(int row) const bool TimeSeries::setCos(int row, const QVariant& value) { + bool set = false; if (!value.canConvert(qMetaTypeId())) { return false; } - bool set = false; set = time_series_set_data_cos(m_d, row, value.value()); if (set) { QModelIndex index = createIndex(row, 0, row); @@ -826,10 +830,10 @@ QVariant TimeSeries::sin(int row) const bool TimeSeries::setSin(int row, const QVariant& value) { + bool set = false; if (!value.canConvert(qMetaTypeId())) { return false; } - bool set = false; set = time_series_set_data_sin(m_d, row, value.value()); if (set) { QModelIndex index = createIndex(row, 0, row); @@ -847,10 +851,10 @@ QVariant TimeSeries::time(int row) const bool TimeSeries::setTime(int row, const QVariant& value) { + bool set = false; if (!value.canConvert(qMetaTypeId())) { return false; } - bool set = false; set = time_series_set_data_time(m_d, row, value.value()); if (set) { QModelIndex index = createIndex(row, 0, row); diff --git a/src/cpp.cpp b/src/cpp.cpp index 841e4e1..7091886 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -165,7 +165,6 @@ void writeModelGetterSetter(QTextStream& cpp, const QString& index, cpp << QString("bool %1::set%2(const QModelIndex& index, const QVariant& value)\n{\n") .arg(o.name, upperInitial(ip.name)); } - cpp << QString(" if (!value.canConvert(qMetaTypeId<%1>())) {\n return false;\n }\n").arg(ip.type.name); cpp << " bool set = false;\n"; if (ip.optional) { QString test = "!value.isValid()"; @@ -175,8 +174,9 @@ void writeModelGetterSetter(QTextStream& cpp, const QString& index, cpp << " if (" << test << ") {\n"; cpp << QString(" set = %1_set_data_%2_none(m_d%3);") .arg(lcname, snakeCase(ip.name), idx) << endl; - cpp << " } else\n"; + cpp << " } else {\n"; } + cpp << QString(" if (!value.canConvert(qMetaTypeId<%1>())) {\n return false;\n }\n").arg(ip.type.name); QString val = QString("value.value<%1>()").arg(ip.type.name); if (ip.type.isComplex()) { cpp << QString(" const %1 s = %2;\n").arg(ip.type.name, val); @@ -188,6 +188,9 @@ void writeModelGetterSetter(QTextStream& cpp, const QString& index, } cpp << QString(" set = %1_set_data_%2(m_d%3, %4);") .arg(lcname, snakeCase(ip.name), idx, val) << endl; + if (ip.optional) { + cpp << " }\n"; + } if (o.type == ObjectType::List) { cpp << R"( if (set) { QModelIndex index = createIndex(row, 0, row); @@ -958,7 +961,7 @@ void writeCpp(const Configuration& conf) { namespace { )").arg(conf.hFile.fileName()); for (auto option: conf.optionalTypes()) { - if (option != "QString") { + if (option != "QString" && option != "QByteArray") { cpp << QString(R"( struct option_%1 { public: @@ -987,8 +990,12 @@ namespace { cpp << R"( typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes); void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) { - v->truncate(0); - v->append(bytes, nbytes); + if (v->isNull() && nbytes == 0) { + *v = QByteArray(bytes, nbytes); + } else { + v->truncate(0); + v->append(bytes, nbytes); + } } )"; } diff --git a/src/rust.cpp b/src/rust.cpp index 05fb27b..e0b0ad8 100644 --- a/src/rust.cpp +++ b/src/rust.cpp @@ -639,7 +639,7 @@ pub extern "C" fn %2_data_%3( set(d, s, data.len() as c_int); } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index); - } else if (ip.type.name == "QString") { + } else if (ip.type.isComplex()) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_data_%3( @@ -691,7 +691,8 @@ pub extern "C" fn %2_set_data_%3( ) -> bool { let o = unsafe { &mut *ptr }; let mut v = Vec::new(); - set_string_from_utf16(&mut v, s, len); + let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, len as usize) }; + v.extend_from_slice(slice); o.set_%3(%5, %6) } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, val); @@ -711,8 +712,8 @@ pub unsafe extern "C" fn %2_set_data_%3( if (ip.write && ip.optional) { r << QString(R"( #[no_mangle] -pub unsafe extern "C" fn %2_set_data_%3_none(ptr: *mut %1, row: c_int%4) -> bool { - (&mut *ptr).set_%3(%5row as usize, None) +pub unsafe extern "C" fn %2_set_data_%3_none(ptr: *mut %1%4) -> bool { + (&mut *ptr).set_%3(%5, None) } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index da0454f..14f44e8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -64,6 +64,7 @@ endfunction(rust_test) rust_test(test_object rust_object) rust_test(test_object_types rust_object_types) rust_test(test_list rust_list) +rust_test(test_list_types rust_list_types) rust_test(test_tree rust_tree) rust_test(test_objects rust_objects) rust_test(test_functions rust_functions) diff --git a/tests/rust_list_types/Cargo.toml b/tests/rust_list_types/Cargo.toml new file mode 100644 index 0000000..2e8f02c --- /dev/null +++ b/tests/rust_list_types/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rust_list" +version = "1.0.0" + +[dependencies] +libc = "*" + +[lib] +name = "rust" +crate-type = ["staticlib"] + +[profile.release] +debug = true diff --git a/tests/rust_list_types/src/implementation.rs b/tests/rust_list_types/src/implementation.rs new file mode 100644 index 0000000..f591a85 --- /dev/null +++ b/tests/rust_list_types/src/implementation.rs @@ -0,0 +1,151 @@ +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] +use interface::*; + +#[derive(Default, Clone)] +struct ListItem { + boolean: bool, + i8: i8, + u8: u8, + i16: i16, + u16: u16, + i32: i32, + u32: u32, + i64: i64, + u64: u64, + f32: f32, + f64: f64, + bytearray: Vec, + optional_bytearray: Option>, + string: String, + optional_string: Option, +} + +pub struct List { + emit: ListEmitter, + model: ListList, + list: Vec, +} + +impl ListTrait for List { + fn new(emit: ListEmitter, model: ListList) -> List { + List { + emit: emit, + model: model, + list: vec![ListItem::default(); 10], + } + } + fn emit(&self) -> &ListEmitter { + &self.emit + } + fn row_count(&self) -> usize { + self.list.len() + } + fn boolean(&self, item: usize) -> bool { + self.list[item].boolean + } + fn set_boolean(&mut self, item: usize, v: bool) -> bool { + self.list[item].boolean = v; + true + } + fn i8(&self, item: usize) -> i8 { + self.list[item].i8 + } + fn set_i8(&mut self, item: usize, v: i8) -> bool { + self.list[item].i8 = v; + true + } + fn u8(&self, item: usize) -> u8 { + self.list[item].u8 + } + fn set_u8(&mut self, item: usize, v: u8) -> bool { + self.list[item].u8 = v; + true + } + fn i16(&self, item: usize) -> i16 { + self.list[item].i16 + } + fn set_i16(&mut self, item: usize, v: i16) -> bool { + self.list[item].i16 = v; + true + } + fn u16(&self, item: usize) -> u16 { + self.list[item].u16 + } + fn set_u16(&mut self, item: usize, v: u16) -> bool { + self.list[item].u16 = v; + true + } + fn i32(&self, item: usize) -> i32 { + self.list[item].i32 + } + fn set_i32(&mut self, item: usize, v: i32) -> bool { + self.list[item].i32 = v; + true + } + fn u32(&self, item: usize) -> u32 { + self.list[item].u32 + } + fn set_u32(&mut self, item: usize, v: u32) -> bool { + self.list[item].u32 = v; + true + } + fn i64(&self, item: usize) -> i64 { + self.list[item].i64 + } + fn set_i64(&mut self, item: usize, v: i64) -> bool { + self.list[item].i64 = v; + true + } + fn u64(&self, item: usize) -> u64 { + self.list[item].u64 + } + fn set_u64(&mut self, item: usize, v: u64) -> bool { + self.list[item].u64 = v; + true + } + fn f32(&self, item: usize) -> f32 { + self.list[item].f32 + } + fn set_f32(&mut self, item: usize, v: f32) -> bool { + self.list[item].f32 = v; + true + } + fn f64(&self, item: usize) -> f64 { + self.list[item].f64 + } + fn set_f64(&mut self, item: usize, v: f64) -> bool { + self.list[item].f64 = v; + true + } + fn string(&self, item: usize) -> &str { + &self.list[item].string + } + fn set_string(&mut self, item: usize, v: String) -> bool { + self.list[item].string = v; + true + } + fn optional_string(&self, item: usize) -> Option<&str> { + self.list[item].optional_string.as_ref().map(|p|&p[..]) + } + fn set_optional_string(&mut self, item: usize, v: Option) -> bool { + self.list[item].optional_string = v; + true + } + fn bytearray(&self, item: usize) -> &[u8] { + &self.list[item].bytearray + } + fn set_bytearray(&mut self, item: usize, v: Vec) -> bool { + self.list[item].bytearray = v; + true + } + fn optional_bytearray(&self, item: usize) -> Option<&[u8]> { + self.list[item].optional_bytearray.as_ref().map(|p|&p[..]) + } + fn set_optional_bytearray(&mut self, item: usize, v: Option>) -> bool { + self.list[item].optional_bytearray = v; + true + } +} + diff --git a/tests/rust_list_types/src/interface.rs b/tests/rust_list_types/src/interface.rs new file mode 100644 index 0000000..63e8ea9 --- /dev/null +++ b/tests/rust_list_types/src/interface.rs @@ -0,0 +1,492 @@ +/* generated by rust_qt_binding_generator */ +#![allow(unknown_lints)] +#![allow(mutex_atomic, needless_pass_by_value)] +use libc::{c_char, c_ushort, c_int, c_void, uint8_t, uint16_t}; +use std::slice; +use std::char::decode_utf16; + +use std::sync::{Arc, Mutex}; +use std::ptr::null; + +use implementation::*; + + +#[repr(C)] +pub struct COption { + data: T, + some: bool, +} + +impl From> for COption +where + T: Default, +{ + fn from(t: Option) -> COption { + if let Some(v) = t { + COption { + data: v, + some: true, + } + } else { + COption { + data: T::default(), + some: false, + } + } + } +} + + +pub enum QString {} + +fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { + let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let characters = decode_utf16(utf16.iter().cloned()) + .into_iter() + .map(|r| r.unwrap()); + s.clear(); + s.extend(characters); +} + + + +pub enum QByteArray {} + + +#[repr(C)] +pub enum SortOrder { + Ascending = 0, + Descending = 1, +} + +#[repr(C)] +pub struct QModelIndex { + row: c_int, + internal_id: usize, +} + +pub struct ListQObject {} + +#[derive(Clone)] +pub struct ListEmitter { + qobject: Arc>, + new_data_ready: fn(*const ListQObject), +} + +unsafe impl Send for ListEmitter {} + +impl ListEmitter { + fn clear(&self) { + *self.qobject.lock().unwrap() = null(); + } + pub fn new_data_ready(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.new_data_ready)(ptr); + } + } +} + +pub struct ListList { + qobject: *const ListQObject, + data_changed: fn(*const ListQObject, usize, usize), + begin_reset_model: fn(*const ListQObject), + end_reset_model: fn(*const ListQObject), + begin_insert_rows: fn(*const ListQObject, usize, usize), + end_insert_rows: fn(*const ListQObject), + begin_remove_rows: fn(*const ListQObject, usize, usize), + end_remove_rows: fn(*const ListQObject), +} + +impl ListList { + pub fn data_changed(&self, first: usize, last: usize) { + (self.data_changed)(self.qobject, first, last); + } + pub fn begin_reset_model(&self) { + (self.begin_reset_model)(self.qobject); + } + pub fn end_reset_model(&self) { + (self.end_reset_model)(self.qobject); + } + pub fn begin_insert_rows(&self, first: usize, last: usize) { + (self.begin_insert_rows)(self.qobject, first, last); + } + pub fn end_insert_rows(&self) { + (self.end_insert_rows)(self.qobject); + } + pub fn begin_remove_rows(&self, first: usize, last: usize) { + (self.begin_remove_rows)(self.qobject, first, last); + } + pub fn end_remove_rows(&self) { + (self.end_remove_rows)(self.qobject); + } +} + +pub trait ListTrait { + fn new(emit: ListEmitter, model: ListList) -> Self; + fn emit(&self) -> &ListEmitter; + fn row_count(&self) -> usize; + fn insert_rows(&mut self, row: usize, count: usize) -> bool { false } + fn remove_rows(&mut self, row: usize, count: usize) -> bool { false } + fn can_fetch_more(&self) -> bool { + false + } + fn fetch_more(&mut self) {} + fn sort(&mut self, u8, SortOrder) {} + fn boolean(&self, item: usize) -> bool; + fn set_boolean(&mut self, item: usize, bool) -> bool; + fn bytearray(&self, item: usize) -> &[u8]; + fn set_bytearray(&mut self, item: usize, Vec) -> bool; + fn f32(&self, item: usize) -> f32; + fn set_f32(&mut self, item: usize, f32) -> bool; + fn f64(&self, item: usize) -> f64; + fn set_f64(&mut self, item: usize, f64) -> bool; + fn i16(&self, item: usize) -> i16; + fn set_i16(&mut self, item: usize, i16) -> bool; + fn i32(&self, item: usize) -> i32; + fn set_i32(&mut self, item: usize, i32) -> bool; + fn i64(&self, item: usize) -> i64; + fn set_i64(&mut self, item: usize, i64) -> bool; + fn i8(&self, item: usize) -> i8; + fn set_i8(&mut self, item: usize, i8) -> bool; + fn optional_bytearray(&self, item: usize) -> Option<&[u8]>; + fn set_optional_bytearray(&mut self, item: usize, Option>) -> bool; + fn optional_string(&self, item: usize) -> Option<&str>; + fn set_optional_string(&mut self, item: usize, Option) -> bool; + fn string(&self, item: usize) -> &str; + fn set_string(&mut self, item: usize, String) -> bool; + fn u16(&self, item: usize) -> u16; + fn set_u16(&mut self, item: usize, u16) -> bool; + fn u32(&self, item: usize) -> u32; + fn set_u32(&mut self, item: usize, u32) -> bool; + fn u64(&self, item: usize) -> u64; + fn set_u64(&mut self, item: usize, u64) -> bool; + fn u8(&self, item: usize) -> u8; + fn set_u8(&mut self, item: usize, u8) -> bool; +} + +#[no_mangle] +pub extern "C" fn list_new( + list: *mut ListQObject, + list_new_data_ready: fn(*const ListQObject), + list_data_changed: fn(*const ListQObject, usize, usize), + list_begin_reset_model: fn(*const ListQObject), + list_end_reset_model: fn(*const ListQObject), + list_begin_insert_rows: fn(*const ListQObject, usize, usize), + list_end_insert_rows: fn(*const ListQObject), + list_begin_remove_rows: fn(*const ListQObject, usize, usize), + list_end_remove_rows: fn(*const ListQObject), +) -> *mut List { + let list_emit = ListEmitter { + qobject: Arc::new(Mutex::new(list)), + new_data_ready: list_new_data_ready, + }; + let model = ListList { + qobject: list, + data_changed: list_data_changed, + begin_reset_model: list_begin_reset_model, + end_reset_model: list_end_reset_model, + begin_insert_rows: list_begin_insert_rows, + end_insert_rows: list_end_insert_rows, + begin_remove_rows: list_begin_remove_rows, + end_remove_rows: list_end_remove_rows, + }; + let d_list = List::new(list_emit, model); + Box::into_raw(Box::new(d_list)) +} + +#[no_mangle] +pub unsafe extern "C" fn list_free(ptr: *mut List) { + Box::from_raw(ptr).emit().clear(); +} + +#[no_mangle] +pub unsafe extern "C" fn list_row_count(ptr: *const List) -> c_int { + (&*ptr).row_count() as c_int +} +#[no_mangle] +pub unsafe extern "C" fn list_insert_rows(ptr: *mut List, row: c_int, count: c_int) -> bool { + (&mut *ptr).insert_rows(row as usize, count as usize) +} +#[no_mangle] +pub unsafe extern "C" fn list_remove_rows(ptr: *mut List, row: c_int, count: c_int) -> bool { + (&mut *ptr).remove_rows(row as usize, count as usize) +} +#[no_mangle] +pub unsafe extern "C" fn list_can_fetch_more(ptr: *const List) -> bool { + (&*ptr).can_fetch_more() +} +#[no_mangle] +pub unsafe extern "C" fn list_fetch_more(ptr: *mut List) { + (&mut *ptr).fetch_more() +} +#[no_mangle] +pub unsafe extern "C" fn list_sort( + ptr: *mut List, + column: u8, + order: SortOrder, +) { + (&mut *ptr).sort(column, order) +} + +#[no_mangle] +pub extern "C" fn list_data_boolean(ptr: *const List, row: c_int) -> bool { + let o = unsafe { &*ptr }; + o.boolean(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_boolean( + ptr: *mut List, row: c_int, + v: bool, +) -> bool { + (&mut *ptr).set_boolean(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_bytearray( + ptr: *const List, row: c_int, + d: *mut QString, + set: fn(*mut QString, *const c_char, len: c_int), +) { + let o = unsafe { &*ptr }; + let data = o.bytearray(row as usize); + let s: *const c_char = data.as_ptr() as (*const c_char); + set(d, s, data.len() as c_int); +} + +#[no_mangle] +pub extern "C" fn list_set_data_bytearray( + ptr: *mut List, row: c_int, + s: *const c_char, len: c_int, +) -> bool { + let o = unsafe { &mut *ptr }; + let mut v = Vec::new(); + let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, len as usize) }; + v.extend_from_slice(slice); + o.set_bytearray(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_f32(ptr: *const List, row: c_int) -> f32 { + let o = unsafe { &*ptr }; + o.f32(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_f32( + ptr: *mut List, row: c_int, + v: f32, +) -> bool { + (&mut *ptr).set_f32(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_f64(ptr: *const List, row: c_int) -> f64 { + let o = unsafe { &*ptr }; + o.f64(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_f64( + ptr: *mut List, row: c_int, + v: f64, +) -> bool { + (&mut *ptr).set_f64(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_i16(ptr: *const List, row: c_int) -> i16 { + let o = unsafe { &*ptr }; + o.i16(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_i16( + ptr: *mut List, row: c_int, + v: i16, +) -> bool { + (&mut *ptr).set_i16(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_i32(ptr: *const List, row: c_int) -> i32 { + let o = unsafe { &*ptr }; + o.i32(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_i32( + ptr: *mut List, row: c_int, + v: i32, +) -> bool { + (&mut *ptr).set_i32(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_i64(ptr: *const List, row: c_int) -> i64 { + let o = unsafe { &*ptr }; + o.i64(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_i64( + ptr: *mut List, row: c_int, + v: i64, +) -> bool { + (&mut *ptr).set_i64(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_i8(ptr: *const List, row: c_int) -> i8 { + let o = unsafe { &*ptr }; + o.i8(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_i8( + ptr: *mut List, row: c_int, + v: i8, +) -> bool { + (&mut *ptr).set_i8(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_optional_bytearray( + ptr: *const List, row: c_int, + d: *mut QString, + set: fn(*mut QString, *const c_char, len: c_int), +) { + let o = unsafe { &*ptr }; + let data = o.optional_bytearray(row as usize); + if let Some(data) = data { + let s: *const c_char = data.as_ptr() as (*const c_char); + set(d, s, data.len() as c_int); + } +} + +#[no_mangle] +pub extern "C" fn list_set_data_optional_bytearray( + ptr: *mut List, row: c_int, + s: *const c_char, len: c_int, +) -> bool { + let o = unsafe { &mut *ptr }; + let mut v = Vec::new(); + let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, len as usize) }; + v.extend_from_slice(slice); + o.set_optional_bytearray(row as usize, Some(v)) +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_optional_bytearray_none(ptr: *mut List, row: c_int) -> bool { + (&mut *ptr).set_optional_bytearray(row as usize, None) +} + +#[no_mangle] +pub extern "C" fn list_data_optional_string( + ptr: *const List, row: c_int, + d: *mut QString, + set: fn(*mut QString, *const c_char, len: c_int), +) { + let o = unsafe { &*ptr }; + let data = o.optional_string(row as usize); + if let Some(data) = data { + let s: *const c_char = data.as_ptr() as (*const c_char); + set(d, s, data.len() as c_int); + } +} + +#[no_mangle] +pub extern "C" fn list_set_data_optional_string( + ptr: *mut List, row: c_int, + s: *const c_ushort, len: c_int, +) -> bool { + let o = unsafe { &mut *ptr }; + let mut v = String::new(); + set_string_from_utf16(&mut v, s, len); + o.set_optional_string(row as usize, Some(v)) +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_optional_string_none(ptr: *mut List, row: c_int) -> bool { + (&mut *ptr).set_optional_string(row as usize, None) +} + +#[no_mangle] +pub extern "C" fn list_data_string( + ptr: *const List, row: c_int, + d: *mut QString, + set: fn(*mut QString, *const c_char, len: c_int), +) { + let o = unsafe { &*ptr }; + let data = o.string(row as usize); + let s: *const c_char = data.as_ptr() as (*const c_char); + set(d, s, data.len() as c_int); +} + +#[no_mangle] +pub extern "C" fn list_set_data_string( + ptr: *mut List, row: c_int, + s: *const c_ushort, len: c_int, +) -> bool { + let o = unsafe { &mut *ptr }; + let mut v = String::new(); + set_string_from_utf16(&mut v, s, len); + o.set_string(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_u16(ptr: *const List, row: c_int) -> u16 { + let o = unsafe { &*ptr }; + o.u16(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_u16( + ptr: *mut List, row: c_int, + v: u16, +) -> bool { + (&mut *ptr).set_u16(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_u32(ptr: *const List, row: c_int) -> u32 { + let o = unsafe { &*ptr }; + o.u32(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_u32( + ptr: *mut List, row: c_int, + v: u32, +) -> bool { + (&mut *ptr).set_u32(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_u64(ptr: *const List, row: c_int) -> u64 { + let o = unsafe { &*ptr }; + o.u64(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_u64( + ptr: *mut List, row: c_int, + v: u64, +) -> bool { + (&mut *ptr).set_u64(row as usize, v) +} + +#[no_mangle] +pub extern "C" fn list_data_u8(ptr: *const List, row: c_int) -> u8 { + let o = unsafe { &*ptr }; + o.u8(row as usize).into() +} + +#[no_mangle] +pub unsafe extern "C" fn list_set_data_u8( + ptr: *mut List, row: c_int, + v: u8, +) -> bool { + (&mut *ptr).set_u8(row as usize, v) +} diff --git a/tests/rust_list_types/src/lib.rs b/tests/rust_list_types/src/lib.rs new file mode 100644 index 0000000..d7eb847 --- /dev/null +++ b/tests/rust_list_types/src/lib.rs @@ -0,0 +1,4 @@ +extern crate libc; + +pub mod interface; +mod implementation; diff --git a/tests/rust_object_types/src/implementation.rs b/tests/rust_object_types/src/implementation.rs index 96ba22d..b70c0d9 100644 --- a/tests/rust_object_types/src/implementation.rs +++ b/tests/rust_object_types/src/implementation.rs @@ -6,27 +6,41 @@ use interface::*; pub struct Object { emit: ObjectEmitter, boolean: bool, - bytearray: Vec, - integer: i32, - optional_bytearray: Option>, - optional_string: Option, - string: String, + i8: i8, + u8: u8, + i16: i16, + u16: u16, + i32: i32, + u32: u32, + i64: i64, u64: u64, - uinteger: u32, + f32: f32, + f64: f64, + bytearray: Vec, + optional_bytearray: Option>, + string: String, + optional_string: Option, } impl ObjectTrait for Object { fn new(emit: ObjectEmitter) -> Object { Object { - emit: emit, - boolean: true, - bytearray: Vec::new(), - integer: 0, - optional_bytearray: None, - optional_string: None, - string: String::new(), + emit, + boolean: false, + i8: 0, + u8: 0, + i16: 0, + u16: 0, + i32: 0, + u32: 0, + i64: 0, u64: 0, - uinteger: 0, + f32: 0., + f64: 0., + bytearray: Vec::new(), + optional_bytearray: None, + string: String::new(), + optional_string: None } } fn emit(&self) -> &ObjectEmitter { @@ -39,6 +53,84 @@ impl ObjectTrait for Object { self.boolean = value; self.emit.boolean_changed(); } + fn i8(&self) -> i8 { + self.i8 + } + fn set_i8(&mut self, value: i8) { + self.i8 = value; + self.emit.i8_changed(); + } + fn u8(&self) -> u8 { + self.u8 + } + fn set_u8(&mut self, value: u8) { + self.u8 = value; + self.emit.u8_changed(); + } + fn i16(&self) -> i16 { + self.i16 + } + fn set_i16(&mut self, value: i16) { + self.i16 = value; + self.emit.i16_changed(); + } + fn u16(&self) -> u16 { + self.u16 + } + fn set_u16(&mut self, value: u16) { + self.u16 = value; + self.emit.u16_changed(); + } + fn i32(&self) -> i32 { + self.i32 + } + fn set_i32(&mut self, value: i32) { + self.i32 = value; + self.emit.i32_changed(); + } + fn u32(&self) -> u32 { + self.u32 + } + fn set_u32(&mut self, value: u32) { + self.u32 = value; + self.emit.u32_changed(); + } + fn i64(&self) -> i64 { + self.i64 + } + fn set_i64(&mut self, value: i64) { + self.i64 = value; + self.emit.i64_changed(); + } + fn u64(&self) -> u64 { + self.u64 + } + fn set_u64(&mut self, value: u64) { + self.u64 = value; + self.emit.u64_changed(); + } + fn f32(&self) -> f32 { + self.f32 + } + fn set_f32(&mut self, value: f32) { + self.f32 = value; + self.emit.f32_changed(); + } + fn f64(&self) -> f64 { + self.f64 + } + fn set_f64(&mut self, value: f64) { + self.f64 = value; + self.emit.f64_changed(); + } + /* + fn optional_boolean(&self) -> Option { + self.optional_boolean + } + fn set_optional_boolean(&mut self, b: Option) { + self.optional_boolean = b; + } + */ fn bytearray(&self) -> &[u8] { &self.bytearray } @@ -46,13 +138,6 @@ impl ObjectTrait for Object { self.bytearray = value; self.emit.bytearray_changed(); } - fn integer(&self) -> i32 { - self.integer - } - fn set_integer(&mut self, value: i32) { - self.integer = value; - self.emit.integer_changed(); - } fn optional_bytearray(&self) -> Option<&[u8]> { self.optional_bytearray.as_ref().map(|p|&p[..]) } @@ -74,19 +159,5 @@ impl ObjectTrait for Object { self.string = value; self.emit.string_changed(); } - fn u64(&self) -> u64 { - self.u64 - } - fn set_u64(&mut self, value: u64) { - self.u64 = value; - self.emit.u64_changed(); - } - fn uinteger(&self) -> u32 { - self.uinteger - } - fn set_uinteger(&mut self, value: u32) { - self.uinteger = value; - self.emit.uinteger_changed(); - } } diff --git a/tests/rust_object_types/src/interface.rs b/tests/rust_object_types/src/interface.rs index abc7f5f..ab06e6a 100644 --- a/tests/rust_object_types/src/interface.rs +++ b/tests/rust_object_types/src/interface.rs @@ -59,12 +59,19 @@ pub struct ObjectEmitter { qobject: Arc>, boolean_changed: fn(*const ObjectQObject), bytearray_changed: fn(*const ObjectQObject), - integer_changed: fn(*const ObjectQObject), + f32_changed: fn(*const ObjectQObject), + f64_changed: fn(*const ObjectQObject), + i16_changed: fn(*const ObjectQObject), + i32_changed: fn(*const ObjectQObject), + i64_changed: fn(*const ObjectQObject), + i8_changed: fn(*const ObjectQObject), optional_bytearray_changed: fn(*const ObjectQObject), optional_string_changed: fn(*const ObjectQObject), string_changed: fn(*const ObjectQObject), + u16_changed: fn(*const ObjectQObject), + u32_changed: fn(*const ObjectQObject), u64_changed: fn(*const ObjectQObject), - uinteger_changed: fn(*const ObjectQObject), + u8_changed: fn(*const ObjectQObject), } unsafe impl Send for ObjectEmitter {} @@ -85,10 +92,40 @@ impl ObjectEmitter { (self.bytearray_changed)(ptr); } } - pub fn integer_changed(&self) { + pub fn f32_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { - (self.integer_changed)(ptr); + (self.f32_changed)(ptr); + } + } + pub fn f64_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.f64_changed)(ptr); + } + } + pub fn i16_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.i16_changed)(ptr); + } + } + pub fn i32_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.i32_changed)(ptr); + } + } + pub fn i64_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.i64_changed)(ptr); + } + } + pub fn i8_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.i8_changed)(ptr); } } pub fn optional_bytearray_changed(&self) { @@ -109,16 +146,28 @@ impl ObjectEmitter { (self.string_changed)(ptr); } } + pub fn u16_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.u16_changed)(ptr); + } + } + pub fn u32_changed(&self) { + let ptr = *self.qobject.lock().unwrap(); + if !ptr.is_null() { + (self.u32_changed)(ptr); + } + } pub fn u64_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.u64_changed)(ptr); } } - pub fn uinteger_changed(&self) { + pub fn u8_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { - (self.uinteger_changed)(ptr); + (self.u8_changed)(ptr); } } } @@ -130,18 +179,32 @@ pub trait ObjectTrait { fn set_boolean(&mut self, value: bool); fn bytearray(&self) -> &[u8]; fn set_bytearray(&mut self, value: Vec); - fn integer(&self) -> i32; - fn set_integer(&mut self, value: i32); + fn f32(&self) -> f32; + fn set_f32(&mut self, value: f32); + fn f64(&self) -> f64; + fn set_f64(&mut self, value: f64); + fn i16(&self) -> i16; + fn set_i16(&mut self, value: i16); + fn i32(&self) -> i32; + fn set_i32(&mut self, value: i32); + fn i64(&self) -> i64; + fn set_i64(&mut self, value: i64); + fn i8(&self) -> i8; + fn set_i8(&mut self, value: i8); fn optional_bytearray(&self) -> Option<&[u8]>; fn set_optional_bytearray(&mut self, value: Option>); fn optional_string(&self) -> Option<&str>; fn set_optional_string(&mut self, value: Option); fn string(&self) -> &str; fn set_string(&mut self, value: String); + fn u16(&self) -> u16; + fn set_u16(&mut self, value: u16); + fn u32(&self) -> u32; + fn set_u32(&mut self, value: u32); fn u64(&self) -> u64; fn set_u64(&mut self, value: u64); - fn uinteger(&self) -> u32; - fn set_uinteger(&mut self, value: u32); + fn u8(&self) -> u8; + fn set_u8(&mut self, value: u8); } #[no_mangle] @@ -149,23 +212,37 @@ pub extern "C" fn object_new( object: *mut ObjectQObject, boolean_changed: fn(*const ObjectQObject), bytearray_changed: fn(*const ObjectQObject), - integer_changed: fn(*const ObjectQObject), + f32_changed: fn(*const ObjectQObject), + f64_changed: fn(*const ObjectQObject), + i16_changed: fn(*const ObjectQObject), + i32_changed: fn(*const ObjectQObject), + i64_changed: fn(*const ObjectQObject), + i8_changed: fn(*const ObjectQObject), optional_bytearray_changed: fn(*const ObjectQObject), optional_string_changed: fn(*const ObjectQObject), string_changed: fn(*const ObjectQObject), + u16_changed: fn(*const ObjectQObject), + u32_changed: fn(*const ObjectQObject), u64_changed: fn(*const ObjectQObject), - uinteger_changed: fn(*const ObjectQObject), + u8_changed: fn(*const ObjectQObject), ) -> *mut Object { let object_emit = ObjectEmitter { qobject: Arc::new(Mutex::new(object)), boolean_changed: boolean_changed, bytearray_changed: bytearray_changed, - integer_changed: integer_changed, + f32_changed: f32_changed, + f64_changed: f64_changed, + i16_changed: i16_changed, + i32_changed: i32_changed, + i64_changed: i64_changed, + i8_changed: i8_changed, optional_bytearray_changed: optional_bytearray_changed, optional_string_changed: optional_string_changed, string_changed: string_changed, + u16_changed: u16_changed, + u32_changed: u32_changed, u64_changed: u64_changed, - uinteger_changed: uinteger_changed, + u8_changed: u8_changed, }; let d_object = Object::new(object_emit); Box::into_raw(Box::new(d_object)) @@ -206,13 +283,63 @@ pub extern "C" fn object_bytearray_set(ptr: *mut Object, v: *const c_char, len: } #[no_mangle] -pub unsafe extern "C" fn object_integer_get(ptr: *const Object) -> i32 { - (&*ptr).integer() +pub unsafe extern "C" fn object_f32_get(ptr: *const Object) -> f32 { + (&*ptr).f32() } #[no_mangle] -pub unsafe extern "C" fn object_integer_set(ptr: *mut Object, v: i32) { - (&mut *ptr).set_integer(v); +pub unsafe extern "C" fn object_f32_set(ptr: *mut Object, v: f32) { + (&mut *ptr).set_f32(v); +} + +#[no_mangle] +pub unsafe extern "C" fn object_f64_get(ptr: *const Object) -> f64 { + (&*ptr).f64() +} + +#[no_mangle] +pub unsafe extern "C" fn object_f64_set(ptr: *mut Object, v: f64) { + (&mut *ptr).set_f64(v); +} + +#[no_mangle] +pub unsafe extern "C" fn object_i16_get(ptr: *const Object) -> i16 { + (&*ptr).i16() +} + +#[no_mangle] +pub unsafe extern "C" fn object_i16_set(ptr: *mut Object, v: i16) { + (&mut *ptr).set_i16(v); +} + +#[no_mangle] +pub unsafe extern "C" fn object_i32_get(ptr: *const Object) -> i32 { + (&*ptr).i32() +} + +#[no_mangle] +pub unsafe extern "C" fn object_i32_set(ptr: *mut Object, v: i32) { + (&mut *ptr).set_i32(v); +} + +#[no_mangle] +pub unsafe extern "C" fn object_i64_get(ptr: *const Object) -> i64 { + (&*ptr).i64() +} + +#[no_mangle] +pub unsafe extern "C" fn object_i64_set(ptr: *mut Object, v: i64) { + (&mut *ptr).set_i64(v); +} + +#[no_mangle] +pub unsafe extern "C" fn object_i8_get(ptr: *const Object) -> i8 { + (&*ptr).i8() +} + +#[no_mangle] +pub unsafe extern "C" fn object_i8_set(ptr: *mut Object, v: i8) { + (&mut *ptr).set_i8(v); } #[no_mangle] @@ -288,6 +415,26 @@ pub extern "C" fn object_string_set(ptr: *mut Object, v: *const c_ushort, len: c o.set_string(s); } +#[no_mangle] +pub unsafe extern "C" fn object_u16_get(ptr: *const Object) -> u16 { + (&*ptr).u16() +} + +#[no_mangle] +pub unsafe extern "C" fn object_u16_set(ptr: *mut Object, v: u16) { + (&mut *ptr).set_u16(v); +} + +#[no_mangle] +pub unsafe extern "C" fn object_u32_get(ptr: *const Object) -> u32 { + (&*ptr).u32() +} + +#[no_mangle] +pub unsafe extern "C" fn object_u32_set(ptr: *mut Object, v: u32) { + (&mut *ptr).set_u32(v); +} + #[no_mangle] pub unsafe extern "C" fn object_u64_get(ptr: *const Object) -> u64 { (&*ptr).u64() @@ -299,11 +446,11 @@ pub unsafe extern "C" fn object_u64_set(ptr: *mut Object, v: u64) { } #[no_mangle] -pub unsafe extern "C" fn object_uinteger_get(ptr: *const Object) -> u32 { - (&*ptr).uinteger() +pub unsafe extern "C" fn object_u8_get(ptr: *const Object) -> u8 { + (&*ptr).u8() } #[no_mangle] -pub unsafe extern "C" fn object_uinteger_set(ptr: *mut Object, v: u32) { - (&mut *ptr).set_uinteger(v); +pub unsafe extern "C" fn object_u8_set(ptr: *mut Object, v: u8) { + (&mut *ptr).set_u8(v); } diff --git a/tests/test_list_rust.cpp b/tests/test_list_rust.cpp index 65181fb..e4a54f1 100644 --- a/tests/test_list_rust.cpp +++ b/tests/test_list_rust.cpp @@ -111,10 +111,10 @@ QVariant Persons::userName(int row) const bool Persons::setUserName(int row, const QVariant& value) { + bool set = false; if (!value.canConvert(qMetaTypeId())) { return false; } - bool set = false; const QString s = value.value(); set = persons_set_data_user_name(m_d, row, s.utf16(), s.length()); if (set) { diff --git a/tests/test_list_types.cpp b/tests/test_list_types.cpp new file mode 100644 index 0000000..451a4ac --- /dev/null +++ b/tests/test_list_types.cpp @@ -0,0 +1,274 @@ +/* + * Copyright 2018 Jos van den Oever + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "test_list_types_rust.h" +#include +#include + +class TestRustListTypes : public QObject +{ + Q_OBJECT +private slots: + void testConstructor(); + void testStringGetter(); + void testStringSetter(); + void testBool(); + void testInt8(); + void testUint8(); + void testInt16(); + void testUint16(); + void testInt32(); + void testUint32(); + void testInt64(); + void testUint64(); + void testFloat(); + void testDouble(); + void testString(); + void testOptionalString(); + void testByteArray(); + void testOptionalByteArray(); +}; + +template +void testSetter(const V v, Set set, Get get) +{ + // GIVEN + List list; + QSignalSpy spy(&list, &List::dataChanged); + + // WHEN + QVariant vv = QVariant::fromValue(v); + if (vv.isNull()) { + vv = QVariant(); + } + bool ok = (list.*set)(0, vv); + QVERIFY(ok); + + // THEN + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QCOMPARE((list.*get)(0), vv); +} + +int getRoleFromName(const QAbstractItemModel& model, const char* name) +{ + auto names = model.roleNames(); + auto i = names.constBegin(); + while (i != names.constEnd()) { + if (i.value() == name) { + return i.key(); + } + ++i; + } + return -1; +} + +template +void testDataSetter(const char* roleName, const V v) +{ + // GIVEN + List list; + QSignalSpy spy(&list, &List::dataChanged); + + // WHEN + int role = getRoleFromName(list, roleName); + auto index = list.index(1, 0); + QVariant vv = QVariant::fromValue(v); + if (vv.isNull()) { + vv = QVariant(); + } + bool ok = list.setData(index, vv, role); + QVERIFY(ok); + + // THEN + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QCOMPARE(list.data(index, role), vv); +} + +template +void test(const V v, Set set, Get get, const char* roleName) +{ + testSetter(v, set, get); + //testDataSetter(roleName, v); +} + +void TestRustListTypes::testConstructor() +{ + List list; +} + +void TestRustListTypes::testBool() +{ + test(true, &List::setBoolean, &List::boolean, "boolean"); + test(false, &List::setBoolean, &List::boolean, "boolean"); +} + +void TestRustListTypes::testInt8() +{ + test(0, &List::setI8, &List::i8, "i8"); + test(1, &List::setI8, &List::i8, "i8"); + test(std::numeric_limits::min(), &List::setI8, &List::i8, "i8"); + test(std::numeric_limits::max(), &List::setI8, &List::i8, "i8"); +} + +void TestRustListTypes::testUint8() +{ + test(0, &List::setU8, &List::u8, "u8"); + test(1, &List::setU8, &List::u8, "u8"); + test(std::numeric_limits::min(), &List::setU8, &List::u8, "u8"); + test(std::numeric_limits::max(), &List::setU8, &List::u8, "u8"); +} + +void TestRustListTypes::testInt16() +{ + test(0, &List::setI16, &List::i16, "i16"); + test(1, &List::setI16, &List::i16, "i16"); + test(std::numeric_limits::min(), &List::setI16, &List::i16, "i16"); + test(std::numeric_limits::max(), &List::setI16, &List::i16, "i16"); +} + +void TestRustListTypes::testUint16() +{ + test(0, &List::setU16, &List::u16, "u16"); + test(1, &List::setU16, &List::u16, "u16"); + test(std::numeric_limits::min(), &List::setU16, &List::u16, "u16"); + test(std::numeric_limits::max(), &List::setU16, &List::u16, "u16"); +} + +void TestRustListTypes::testInt32() +{ + test(0, &List::setI32, &List::i32, "i32"); + test(1, &List::setI32, &List::i32, "i32"); + test(std::numeric_limits::min(), &List::setI32, &List::i32, "i32"); + test(std::numeric_limits::max(), &List::setI32, &List::i32, "i32"); +} + +void TestRustListTypes::testUint32() +{ + test(0, &List::setU32, &List::u32, "u32"); + test(1, &List::setU32, &List::u32, "u32"); + test(std::numeric_limits::min(), &List::setU32, &List::u32, "u32"); + test(std::numeric_limits::max(), &List::setU32, &List::u32, "u32"); +} + +void TestRustListTypes::testInt64() +{ + test(0, &List::setI64, &List::i64, "i64"); + test(1, &List::setI64, &List::i64, "i64"); + test(std::numeric_limits::min(), &List::setI64, &List::i64, "i64"); + test(std::numeric_limits::max(), &List::setI64, &List::i64, "i64"); +} + +void TestRustListTypes::testUint64() +{ + test(0, &List::setU64, &List::u64, "u64"); + test(1, &List::setU64, &List::u64, "u64"); + test(std::numeric_limits::min(), &List::setU64, &List::u64, "u64"); + test(std::numeric_limits::max(), &List::setU64, &List::u64, "u64"); +} + +void TestRustListTypes::testFloat() +{ + test(0, &List::setF32, &List::f32, "f32"); + test(1, &List::setF32, &List::f32, "f32"); + test(std::numeric_limits::min(), &List::setF32, &List::f32, "f32"); + test(std::numeric_limits::max(), &List::setF32, &List::f32, "f32"); +} + +void TestRustListTypes::testDouble() +{ + test(0, &List::setF64, &List::f64, "f64"); + test(1, &List::setF64, &List::f64, "f64"); + test(std::numeric_limits::min(), &List::setF64, &List::f64, "f64"); + test(std::numeric_limits::max(), &List::setF64, &List::f64, "f64"); +} + +void TestRustListTypes::testString() +{ + test(QString(""), &List::setString, &List::string, "string"); + test(QString("Konqi"), &List::setString, &List::string, "string"); + test(QString("$𐐷𤭢"), &List::setString, &List::string, "string"); +} + +void TestRustListTypes::testOptionalString() +{ + test(QString(), &List::setOptionalString, &List::optionalString, + "optionalString"); + test(QString(""), &List::setOptionalString, &List::optionalString, + "optionalString"); + test(QString("Konqi"), &List::setOptionalString, &List::optionalString, + "optionalString"); + test(QString("$𐐷𤭢"), &List::setOptionalString, &List::optionalString, + "optionalString"); +} + +void TestRustListTypes::testByteArray() +{ + const char data[10] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9}; + test(QByteArray(data, 0), &List::setBytearray, + &List::bytearray, "bytearray"); + test(QByteArray(data, 10), &List::setBytearray, + &List::bytearray, "bytearray"); +} + +void TestRustListTypes::testOptionalByteArray() +{ + test(QByteArray(), &List::setOptionalBytearray, &List::optionalBytearray, + "optionalBytearray"); + const char data[10] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9}; + test(QByteArray(data, 0), &List::setOptionalBytearray, + &List::optionalBytearray, "optionalBytearray"); + test(QByteArray(data, 10), &List::setOptionalBytearray, + &List::optionalBytearray, "optionalBytearray"); +} + + +void TestRustListTypes::testStringGetter() +{ + List list; + QCOMPARE(list.rowCount(), 10); + QVariant value = list.data(list.index(0,0)); + // value should be empty string in default implementation + QVERIFY(value.isValid()); + QCOMPARE(value.type(), QVariant::String); + QCOMPARE(value.toString(), QString()); +} + +void TestRustListTypes::testStringSetter() +{ + // GIVEN + List list; + QSignalSpy spy(&list, &List::dataChanged); + + // WHEN + const QModelIndex index(list.index(0,0)); + const bool set = list.setData(index, "Konqi"); + + // THEN + QVERIFY(set); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QVariant value = list.data(list.index(0,0)); + QCOMPARE(value.toString(), QString("Konqi")); +} + +QTEST_MAIN(TestRustListTypes) +#include "test_list_types.moc" diff --git a/tests/test_list_types.json b/tests/test_list_types.json new file mode 100644 index 0000000..6f4b2a5 --- /dev/null +++ b/tests/test_list_types.json @@ -0,0 +1,78 @@ +{ + "cppFile": "test_list_types_rust.cpp", + "rust": { + "dir": "rust_list_types", + "interfaceModule": "interface", + "implementationModule": "implementation" + }, + "objects": { + "List": { + "type": "List", + "itemProperties": { + "boolean": { + "type": "bool", + "write": true + }, + "i8": { + "type": "qint8", + "write": true + }, + "u8": { + "type": "quint8", + "write": true + }, + "i16": { + "type": "qint16", + "write": true + }, + "u16": { + "type": "quint16", + "write": true + }, + "i32": { + "type": "qint32", + "write": true + }, + "u32": { + "type": "quint32", + "write": true + }, + "i64": { + "type": "qint64", + "write": true + }, + "u64": { + "type": "quint64", + "write": true + }, + "f32": { + "type": "float", + "write": true + }, + "f64": { + "type": "double", + "write": true + }, + "string": { + "type": "QString", + "write": true, + "roles": [ [ "display", "edit" ] ] + }, + "optionalString": { + "type": "QString", + "write": true, + "optional": true + }, + "bytearray": { + "type": "QByteArray", + "write": true + }, + "optionalBytearray": { + "type": "QByteArray", + "write": true, + "optional": true + } + } + } + } +} diff --git a/tests/test_list_types_rust.cpp b/tests/test_list_types_rust.cpp new file mode 100644 index 0000000..50b79a6 --- /dev/null +++ b/tests/test_list_types_rust.cpp @@ -0,0 +1,674 @@ +/* generated by rust_qt_binding_generator */ +#include "test_list_types_rust.h" + +namespace { + + struct option_quintptr { + public: + quintptr value; + bool some; + operator QVariant() const { + if (some) { + return QVariant::fromValue(value); + } + return QVariant(); + } + }; + static_assert(std::is_pod::value, "option_quintptr must be a POD type."); + + typedef void (*qstring_set)(QString* val, const char* utf8, int nbytes); + void set_qstring(QString* val, const char* utf8, int nbytes) { + *val = QString::fromUtf8(utf8, nbytes); + } + + typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes); + void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) { + if (v->isNull() && nbytes == 0) { + *v = QByteArray(bytes, nbytes); + } else { + v->truncate(0); + v->append(bytes, nbytes); + } + } + + struct qmodelindex_t { + int row; + quintptr id; + }; +} +extern "C" { + bool list_data_boolean(const List::Private*, int); + bool list_set_data_boolean(List::Private*, int, bool); + void list_data_bytearray(const List::Private*, int, QByteArray*, qbytearray_set); + bool list_set_data_bytearray(List::Private*, int, const char* s, int len); + float list_data_f32(const List::Private*, int); + bool list_set_data_f32(List::Private*, int, float); + double list_data_f64(const List::Private*, int); + bool list_set_data_f64(List::Private*, int, double); + qint16 list_data_i16(const List::Private*, int); + bool list_set_data_i16(List::Private*, int, qint16); + qint32 list_data_i32(const List::Private*, int); + bool list_set_data_i32(List::Private*, int, qint32); + qint64 list_data_i64(const List::Private*, int); + bool list_set_data_i64(List::Private*, int, qint64); + qint8 list_data_i8(const List::Private*, int); + bool list_set_data_i8(List::Private*, int, qint8); + void list_data_optional_bytearray(const List::Private*, int, QByteArray*, qbytearray_set); + bool list_set_data_optional_bytearray(List::Private*, int, const char* s, int len); + bool list_set_data_optional_bytearray_none(List::Private*, int); + void list_data_optional_string(const List::Private*, int, QString*, qstring_set); + bool list_set_data_optional_string(List::Private*, int, const ushort* s, int len); + bool list_set_data_optional_string_none(List::Private*, int); + void list_data_string(const List::Private*, int, QString*, qstring_set); + bool list_set_data_string(List::Private*, int, const ushort* s, int len); + uint list_data_u16(const List::Private*, int); + bool list_set_data_u16(List::Private*, int, uint); + uint list_data_u32(const List::Private*, int); + bool list_set_data_u32(List::Private*, int, uint); + quint64 list_data_u64(const List::Private*, int); + bool list_set_data_u64(List::Private*, int, quint64); + quint8 list_data_u8(const List::Private*, int); + bool list_set_data_u8(List::Private*, int, quint8); + void list_sort(List::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder); + + int list_row_count(const List::Private*); + bool list_insert_rows(List::Private*, int, int); + bool list_remove_rows(List::Private*, int, int); + bool list_can_fetch_more(const List::Private*); + void list_fetch_more(List::Private*); +} +int List::columnCount(const QModelIndex &parent) const +{ + return (parent.isValid()) ? 0 : 1; +} + +bool List::hasChildren(const QModelIndex &parent) const +{ + return rowCount(parent) > 0; +} + +int List::rowCount(const QModelIndex &parent) const +{ + return (parent.isValid()) ? 0 : list_row_count(m_d); +} + +bool List::insertRows(int row, int count, const QModelIndex &parent) +{ + return list_insert_rows(m_d, row, count); +} + +bool List::removeRows(int row, int count, const QModelIndex &parent) +{ + return list_remove_rows(m_d, row, count); +} + +QModelIndex List::index(int row, int column, const QModelIndex &parent) const +{ + if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) { + return createIndex(row, column, (quintptr)row); + } + return QModelIndex(); +} + +QModelIndex List::parent(const QModelIndex &) const +{ + return QModelIndex(); +} + +bool List::canFetchMore(const QModelIndex &parent) const +{ + return (parent.isValid()) ? 0 : list_can_fetch_more(m_d); +} + +void List::fetchMore(const QModelIndex &parent) +{ + if (!parent.isValid()) { + list_fetch_more(m_d); + } +} + +void List::sort(int column, Qt::SortOrder order) +{ + list_sort(m_d, column, order); +} +Qt::ItemFlags List::flags(const QModelIndex &i) const +{ + auto flags = QAbstractItemModel::flags(i); + if (i.column() == 0) { + flags |= Qt::ItemIsEditable; + } + return flags; +} + +QVariant List::boolean(int row) const +{ + QVariant v; + v.setValue(list_data_boolean(m_d, row)); + return v; +} + +bool List::setBoolean(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_boolean(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::bytearray(int row) const +{ + QVariant v; + QByteArray b; + list_data_bytearray(m_d, row, &b, set_qbytearray); + if (!b.isNull()) v.setValue(b); + return v; +} + +bool List::setBytearray(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + const QByteArray s = value.value(); + set = list_set_data_bytearray(m_d, row, s.data(), s.length()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::f32(int row) const +{ + QVariant v; + v.setValue(list_data_f32(m_d, row)); + return v; +} + +bool List::setF32(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_f32(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::f64(int row) const +{ + QVariant v; + v.setValue(list_data_f64(m_d, row)); + return v; +} + +bool List::setF64(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_f64(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::i16(int row) const +{ + QVariant v; + v.setValue(list_data_i16(m_d, row)); + return v; +} + +bool List::setI16(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_i16(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::i32(int row) const +{ + QVariant v; + v.setValue(list_data_i32(m_d, row)); + return v; +} + +bool List::setI32(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_i32(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::i64(int row) const +{ + QVariant v; + v.setValue(list_data_i64(m_d, row)); + return v; +} + +bool List::setI64(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_i64(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::i8(int row) const +{ + QVariant v; + v.setValue(list_data_i8(m_d, row)); + return v; +} + +bool List::setI8(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_i8(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::optionalBytearray(int row) const +{ + QVariant v; + QByteArray b; + list_data_optional_bytearray(m_d, row, &b, set_qbytearray); + if (!b.isNull()) v.setValue(b); + return v; +} + +bool List::setOptionalBytearray(int row, const QVariant& value) +{ + bool set = false; + if (!value.isValid() || value.isNull()) { + set = list_set_data_optional_bytearray_none(m_d, row); + } else { + if (!value.canConvert(qMetaTypeId())) { + return false; + } + const QByteArray s = value.value(); + set = list_set_data_optional_bytearray(m_d, row, s.data(), s.length()); + } + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::optionalString(int row) const +{ + QVariant v; + QString s; + list_data_optional_string(m_d, row, &s, set_qstring); + if (!s.isNull()) v.setValue(s); + return v; +} + +bool List::setOptionalString(int row, const QVariant& value) +{ + bool set = false; + if (!value.isValid() || value.isNull()) { + set = list_set_data_optional_string_none(m_d, row); + } else { + if (!value.canConvert(qMetaTypeId())) { + return false; + } + const QString s = value.value(); + set = list_set_data_optional_string(m_d, row, s.utf16(), s.length()); + } + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::string(int row) const +{ + QVariant v; + QString s; + list_data_string(m_d, row, &s, set_qstring); + if (!s.isNull()) v.setValue(s); + return v; +} + +bool List::setString(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + const QString s = value.value(); + set = list_set_data_string(m_d, row, s.utf16(), s.length()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::u16(int row) const +{ + QVariant v; + v.setValue(list_data_u16(m_d, row)); + return v; +} + +bool List::setU16(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_u16(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::u32(int row) const +{ + QVariant v; + v.setValue(list_data_u32(m_d, row)); + return v; +} + +bool List::setU32(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_u32(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::u64(int row) const +{ + QVariant v; + v.setValue(list_data_u64(m_d, row)); + return v; +} + +bool List::setU64(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_u64(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::u8(int row) const +{ + QVariant v; + v.setValue(list_data_u8(m_d, row)); + return v; +} + +bool List::setU8(int row, const QVariant& value) +{ + bool set = false; + if (!value.canConvert(qMetaTypeId())) { + return false; + } + set = list_set_data_u8(m_d, row, value.value()); + if (set) { + QModelIndex index = createIndex(row, 0, row); + emit dataChanged(index, index); + } + return set; +} + +QVariant List::data(const QModelIndex &index, int role) const +{ + Q_ASSERT(rowCount(index.parent()) > index.row()); + switch (index.column()) { + case 0: + switch (role) { + case Qt::UserRole + 0: + return boolean(index.row()); + case Qt::UserRole + 1: + return bytearray(index.row()); + case Qt::UserRole + 2: + return f32(index.row()); + case Qt::UserRole + 3: + return f64(index.row()); + case Qt::UserRole + 4: + return i16(index.row()); + case Qt::UserRole + 5: + return i32(index.row()); + case Qt::UserRole + 6: + return i64(index.row()); + case Qt::UserRole + 7: + return i8(index.row()); + case Qt::UserRole + 8: + return optionalBytearray(index.row()); + case Qt::UserRole + 9: + return optionalString(index.row()); + case Qt::DisplayRole: + case Qt::EditRole: + case Qt::UserRole + 10: + return string(index.row()); + case Qt::UserRole + 11: + return u16(index.row()); + case Qt::UserRole + 12: + return u32(index.row()); + case Qt::UserRole + 13: + return u64(index.row()); + case Qt::UserRole + 14: + return u8(index.row()); + } + } + return QVariant(); +} + +QHash List::roleNames() const { + QHash names = QAbstractItemModel::roleNames(); + names.insert(Qt::UserRole + 0, "boolean"); + names.insert(Qt::UserRole + 1, "bytearray"); + names.insert(Qt::UserRole + 2, "f32"); + names.insert(Qt::UserRole + 3, "f64"); + names.insert(Qt::UserRole + 4, "i16"); + names.insert(Qt::UserRole + 5, "i32"); + names.insert(Qt::UserRole + 6, "i64"); + names.insert(Qt::UserRole + 7, "i8"); + names.insert(Qt::UserRole + 8, "optionalBytearray"); + names.insert(Qt::UserRole + 9, "optionalString"); + names.insert(Qt::UserRole + 10, "string"); + names.insert(Qt::UserRole + 11, "u16"); + names.insert(Qt::UserRole + 12, "u32"); + names.insert(Qt::UserRole + 13, "u64"); + names.insert(Qt::UserRole + 14, "u8"); + return names; +} +QVariant List::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) { + return QVariant(); + } + return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant()); +} + +bool List::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) +{ + if (orientation != Qt::Horizontal) { + return false; + } + m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value); + return true; +} + +bool List::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.column() == 0) { + if (role == Qt::UserRole + 0) { + return setBoolean(index.row(), value); + } + if (role == Qt::UserRole + 1) { + return setBytearray(index.row(), value); + } + if (role == Qt::UserRole + 2) { + return setF32(index.row(), value); + } + if (role == Qt::UserRole + 3) { + return setF64(index.row(), value); + } + if (role == Qt::UserRole + 4) { + return setI16(index.row(), value); + } + if (role == Qt::UserRole + 5) { + return setI32(index.row(), value); + } + if (role == Qt::UserRole + 6) { + return setI64(index.row(), value); + } + if (role == Qt::UserRole + 7) { + return setI8(index.row(), value); + } + if (role == Qt::UserRole + 8) { + return setOptionalBytearray(index.row(), value); + } + if (role == Qt::UserRole + 9) { + return setOptionalString(index.row(), value); + } + if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 10) { + return setString(index.row(), value); + } + if (role == Qt::UserRole + 11) { + return setU16(index.row(), value); + } + if (role == Qt::UserRole + 12) { + return setU32(index.row(), value); + } + if (role == Qt::UserRole + 13) { + return setU64(index.row(), value); + } + if (role == Qt::UserRole + 14) { + return setU8(index.row(), value); + } + } + return false; +} + +extern "C" { + List::Private* list_new(List*, + void (*)(const List*), + void (*)(List*, quintptr, quintptr), + void (*)(List*), + void (*)(List*), + void (*)(List*, int, int), + void (*)(List*), + void (*)(List*, int, int), + void (*)(List*)); + void list_free(List::Private*); +}; + +List::List(bool /*owned*/, QObject *parent): + QAbstractItemModel(parent), + m_d(0), + m_ownsPrivate(false) +{ + initHeaderData(); +} + +List::List(QObject *parent): + QAbstractItemModel(parent), + m_d(list_new(this, + [](const List* o) { + emit o->newDataReady(QModelIndex()); + }, + [](List* o, quintptr first, quintptr last) { + o->dataChanged(o->createIndex(first, 0, first), + o->createIndex(last, 0, last)); + }, + [](List* o) { + o->beginResetModel(); + }, + [](List* o) { + o->endResetModel(); + }, + [](List* o, int first, int last) { + o->beginInsertRows(QModelIndex(), first, last); + }, + [](List* o) { + o->endInsertRows(); + }, + [](List* o, int first, int last) { + o->beginRemoveRows(QModelIndex(), first, last); + }, + [](List* o) { + o->endRemoveRows(); + } +)), + m_ownsPrivate(true) +{ + connect(this, &List::newDataReady, this, [this](const QModelIndex& i) { + this->fetchMore(i); + }, Qt::QueuedConnection); + initHeaderData(); +} + +List::~List() { + if (m_ownsPrivate) { + list_free(m_d); + } +} +void List::initHeaderData() { + m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("string")); +} diff --git a/tests/test_list_types_rust.h b/tests/test_list_types_rust.h new file mode 100644 index 0000000..1c84341 --- /dev/null +++ b/tests/test_list_types_rust.h @@ -0,0 +1,78 @@ +/* generated by rust_qt_binding_generator */ +#ifndef TEST_LIST_TYPES_RUST_H +#define TEST_LIST_TYPES_RUST_H + +#include +#include + +class List; + +class List : public QAbstractItemModel +{ + Q_OBJECT +public: + class Private; +private: + Private * m_d; + bool m_ownsPrivate; + explicit List(bool owned, QObject *parent); +public: + explicit List(QObject *parent = nullptr); + ~List(); + + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex parent(const QModelIndex &index) const override; + bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + bool canFetchMore(const QModelIndex &parent) const override; + void fetchMore(const QModelIndex &parent) override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + QHash roleNames() const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override; + Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + Q_INVOKABLE QVariant boolean(int row) const; + Q_INVOKABLE bool setBoolean(int row, const QVariant& value); + Q_INVOKABLE QVariant bytearray(int row) const; + Q_INVOKABLE bool setBytearray(int row, const QVariant& value); + Q_INVOKABLE QVariant f32(int row) const; + Q_INVOKABLE bool setF32(int row, const QVariant& value); + Q_INVOKABLE QVariant f64(int row) const; + Q_INVOKABLE bool setF64(int row, const QVariant& value); + Q_INVOKABLE QVariant i16(int row) const; + Q_INVOKABLE bool setI16(int row, const QVariant& value); + Q_INVOKABLE QVariant i32(int row) const; + Q_INVOKABLE bool setI32(int row, const QVariant& value); + Q_INVOKABLE QVariant i64(int row) const; + Q_INVOKABLE bool setI64(int row, const QVariant& value); + Q_INVOKABLE QVariant i8(int row) const; + Q_INVOKABLE bool setI8(int row, const QVariant& value); + Q_INVOKABLE QVariant optionalBytearray(int row) const; + Q_INVOKABLE bool setOptionalBytearray(int row, const QVariant& value); + Q_INVOKABLE QVariant optionalString(int row) const; + Q_INVOKABLE bool setOptionalString(int row, const QVariant& value); + Q_INVOKABLE QVariant string(int row) const; + Q_INVOKABLE bool setString(int row, const QVariant& value); + Q_INVOKABLE QVariant u16(int row) const; + Q_INVOKABLE bool setU16(int row, const QVariant& value); + Q_INVOKABLE QVariant u32(int row) const; + Q_INVOKABLE bool setU32(int row, const QVariant& value); + Q_INVOKABLE QVariant u64(int row) const; + Q_INVOKABLE bool setU64(int row, const QVariant& value); + Q_INVOKABLE QVariant u8(int row) const; + Q_INVOKABLE bool setU8(int row, const QVariant& value); + +signals: + // new data is ready to be made available to the model with fetchMore() + void newDataReady(const QModelIndex &parent) const; +private: + QHash, QVariant> m_headerData; + void initHeaderData(); +signals: +}; +#endif // TEST_LIST_TYPES_RUST_H diff --git a/tests/test_object_types.cpp b/tests/test_object_types.cpp index d194b47..682a6bb 100644 --- a/tests/test_object_types.cpp +++ b/tests/test_object_types.cpp @@ -8,9 +8,16 @@ class TestRustObjectTypes : public QObject Q_OBJECT private slots: void testBool(); - void testInteger(); - void testUinteger(); + void testInt8(); + void testUint8(); + void testInt16(); + void testUint16(); + void testInt32(); + void testUint32(); + void testInt64(); void testUint64(); + void testFloat(); + void testDouble(); void testString(); void testOptionalString(); void testByteArray(); @@ -41,28 +48,88 @@ void TestRustObjectTypes::testBool() &Object::boolean, &Object::booleanChanged); } -void TestRustObjectTypes::testInteger() +void TestRustObjectTypes::testInt8() { - testSetter(0, &Object::setInteger, - &Object::integer, &Object::integerChanged); - testSetter(1, &Object::setInteger, - &Object::integer, &Object::integerChanged); - testSetter(std::numeric_limits::min(), &Object::setInteger, - &Object::integer, &Object::integerChanged); - testSetter(std::numeric_limits::max(), &Object::setInteger, - &Object::integer, &Object::integerChanged); + testSetter(0, &Object::setI8, + &Object::i8, &Object::i8Changed); + testSetter(1, &Object::setI8, + &Object::i8, &Object::i8Changed); + testSetter(std::numeric_limits::min(), &Object::setI8, + &Object::i8, &Object::i8Changed); + testSetter(std::numeric_limits::max(), &Object::setI8, + &Object::i8, &Object::i8Changed); } -void TestRustObjectTypes::testUinteger() +void TestRustObjectTypes::testUint8() { - testSetter(0, &Object::setUinteger, - &Object::uinteger, &Object::uintegerChanged); - testSetter(1, &Object::setUinteger, - &Object::uinteger, &Object::uintegerChanged); - testSetter(std::numeric_limits::min(), &Object::setUinteger, - &Object::uinteger, &Object::uintegerChanged); - testSetter(std::numeric_limits::max(), &Object::setUinteger, - &Object::uinteger, &Object::uintegerChanged); + testSetter(0, &Object::setU8, + &Object::u8, &Object::u8Changed); + testSetter(1, &Object::setU8, + &Object::u8, &Object::u8Changed); + testSetter(std::numeric_limits::min(), &Object::setU8, + &Object::u8, &Object::u8Changed); + testSetter(std::numeric_limits::max(), &Object::setU8, + &Object::u8, &Object::u8Changed); +} + +void TestRustObjectTypes::testInt16() +{ + testSetter(0, &Object::setI16, + &Object::i16, &Object::i16Changed); + testSetter(1, &Object::setI16, + &Object::i16, &Object::i16Changed); + testSetter(std::numeric_limits::min(), &Object::setI16, + &Object::i16, &Object::i16Changed); + testSetter(std::numeric_limits::max(), &Object::setI16, + &Object::i16, &Object::i16Changed); +} + +void TestRustObjectTypes::testUint16() +{ + testSetter(0, &Object::setU16, + &Object::u16, &Object::u16Changed); + testSetter(1, &Object::setU16, + &Object::u16, &Object::u16Changed); + testSetter(std::numeric_limits::min(), &Object::setU16, + &Object::u16, &Object::u16Changed); + testSetter(std::numeric_limits::max(), &Object::setU16, + &Object::u16, &Object::u16Changed); +} + +void TestRustObjectTypes::testInt32() +{ + testSetter(0, &Object::setI32, + &Object::i32, &Object::i32Changed); + testSetter(1, &Object::setI32, + &Object::i32, &Object::i32Changed); + testSetter(std::numeric_limits::min(), &Object::setI32, + &Object::i32, &Object::i32Changed); + testSetter(std::numeric_limits::max(), &Object::setI32, + &Object::i32, &Object::i32Changed); +} + +void TestRustObjectTypes::testUint32() +{ + testSetter(0, &Object::setU32, + &Object::u32, &Object::u32Changed); + testSetter(1, &Object::setU32, + &Object::u32, &Object::u32Changed); + testSetter(std::numeric_limits::min(), &Object::setU32, + &Object::u32, &Object::u32Changed); + testSetter(std::numeric_limits::max(), &Object::setU32, + &Object::u32, &Object::u32Changed); +} + +void TestRustObjectTypes::testInt64() +{ + testSetter(0, &Object::setI64, + &Object::i64, &Object::i64Changed); + testSetter(1, &Object::setI64, + &Object::i64, &Object::i64Changed); + testSetter(std::numeric_limits::min(), &Object::setI64, + &Object::i64, &Object::i64Changed); + testSetter(std::numeric_limits::max(), &Object::setI64, + &Object::i64, &Object::i64Changed); } void TestRustObjectTypes::testUint64() @@ -71,12 +138,36 @@ void TestRustObjectTypes::testUint64() &Object::u64, &Object::u64Changed); testSetter(1, &Object::setU64, &Object::u64, &Object::u64Changed); - testSetter(std::numeric_limits::min(), &Object::setU64, + testSetter(std::numeric_limits::min(), &Object::setU64, &Object::u64, &Object::u64Changed); - testSetter(std::numeric_limits::max(), &Object::setU64, + testSetter(std::numeric_limits::max(), &Object::setU64, &Object::u64, &Object::u64Changed); } +void TestRustObjectTypes::testFloat() +{ + testSetter(0, &Object::setF32, + &Object::f32, &Object::f32Changed); + testSetter(1, &Object::setF32, + &Object::f32, &Object::f32Changed); + testSetter(std::numeric_limits::min(), &Object::setF32, + &Object::f32, &Object::f32Changed); + testSetter(std::numeric_limits::max(), &Object::setF32, + &Object::f32, &Object::f32Changed); +} + +void TestRustObjectTypes::testDouble() +{ + testSetter(0, &Object::setF64, + &Object::f64, &Object::f64Changed); + testSetter(1, &Object::setF64, + &Object::f64, &Object::f64Changed); + testSetter(std::numeric_limits::min(), &Object::setF64, + &Object::f64, &Object::f64Changed); + testSetter(std::numeric_limits::max(), &Object::setF64, + &Object::f64, &Object::f64Changed); +} + void TestRustObjectTypes::testString() { testSetter(QString(), &Object::setString, diff --git a/tests/test_object_types.json b/tests/test_object_types.json index 5fdb62f..656d149 100644 --- a/tests/test_object_types.json +++ b/tests/test_object_types.json @@ -13,18 +13,46 @@ "type": "bool", "write": true }, - "integer": { + "i8": { + "type": "qint8", + "write": true + }, + "u8": { + "type": "quint8", + "write": true + }, + "i16": { + "type": "qint16", + "write": true + }, + "u16": { + "type": "quint16", + "write": true + }, + "i32": { "type": "qint32", "write": true }, - "uinteger": { + "u32": { "type": "quint32", "write": true }, + "i64": { + "type": "qint64", + "write": true + }, "u64": { "type": "quint64", "write": true }, + "f32": { + "type": "float", + "write": true + }, + "f64": { + "type": "double", + "write": true + }, "string": { "type": "QString", "write": true diff --git a/tests/test_object_types_rust.cpp b/tests/test_object_types_rust.cpp index b095f23..4544677 100644 --- a/tests/test_object_types_rust.cpp +++ b/tests/test_object_types_rust.cpp @@ -10,8 +10,12 @@ namespace { typedef void (*qbytearray_set)(QByteArray* val, const char* bytes, int nbytes); void set_qbytearray(QByteArray* v, const char* bytes, int nbytes) { - v->truncate(0); - v->append(bytes, nbytes); + if (v->isNull() && nbytes == 0) { + *v = QByteArray(bytes, nbytes); + } else { + v->truncate(0); + v->append(bytes, nbytes); + } } inline void objectBooleanChanged(Object* o) { @@ -21,9 +25,29 @@ namespace { { emit o->bytearrayChanged(); } - inline void objectIntegerChanged(Object* o) + inline void objectF32Changed(Object* o) { - emit o->integerChanged(); + emit o->f32Changed(); + } + inline void objectF64Changed(Object* o) + { + emit o->f64Changed(); + } + inline void objectI16Changed(Object* o) + { + emit o->i16Changed(); + } + inline void objectI32Changed(Object* o) + { + emit o->i32Changed(); + } + inline void objectI64Changed(Object* o) + { + emit o->i64Changed(); + } + inline void objectI8Changed(Object* o) + { + emit o->i8Changed(); } inline void objectOptionalBytearrayChanged(Object* o) { @@ -37,24 +61,42 @@ namespace { { emit o->stringChanged(); } + inline void objectU16Changed(Object* o) + { + emit o->u16Changed(); + } + inline void objectU32Changed(Object* o) + { + emit o->u32Changed(); + } inline void objectU64Changed(Object* o) { emit o->u64Changed(); } - inline void objectUintegerChanged(Object* o) + inline void objectU8Changed(Object* o) { - emit o->uintegerChanged(); + emit o->u8Changed(); } } extern "C" { - Object::Private* object_new(Object*, void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*)); + Object::Private* object_new(Object*, void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*), void (*)(Object*)); void object_free(Object::Private*); bool object_boolean_get(const Object::Private*); void object_boolean_set(Object::Private*, bool); void object_bytearray_get(const Object::Private*, QByteArray*, qbytearray_set); void object_bytearray_set(Object::Private*, const char* bytes, int len); - qint32 object_integer_get(const Object::Private*); - void object_integer_set(Object::Private*, qint32); + float object_f32_get(const Object::Private*); + void object_f32_set(Object::Private*, float); + double object_f64_get(const Object::Private*); + void object_f64_set(Object::Private*, double); + qint16 object_i16_get(const Object::Private*); + void object_i16_set(Object::Private*, qint16); + qint32 object_i32_get(const Object::Private*); + void object_i32_set(Object::Private*, qint32); + qint64 object_i64_get(const Object::Private*); + void object_i64_set(Object::Private*, qint64); + qint8 object_i8_get(const Object::Private*); + void object_i8_set(Object::Private*, qint8); void object_optional_bytearray_get(const Object::Private*, QByteArray*, qbytearray_set); void object_optional_bytearray_set(Object::Private*, const char* bytes, int len); void object_optional_bytearray_set_none(Object::Private*); @@ -63,10 +105,14 @@ extern "C" { void object_optional_string_set_none(Object::Private*); void object_string_get(const Object::Private*, QString*, qstring_set); void object_string_set(Object::Private*, const ushort *str, int len); + quint16 object_u16_get(const Object::Private*); + void object_u16_set(Object::Private*, uint); + quint32 object_u32_get(const Object::Private*); + void object_u32_set(Object::Private*, uint); quint64 object_u64_get(const Object::Private*); void object_u64_set(Object::Private*, quint64); - quint32 object_uinteger_get(const Object::Private*); - void object_uinteger_set(Object::Private*, uint); + quint8 object_u8_get(const Object::Private*); + void object_u8_set(Object::Private*, quint8); }; Object::Object(bool /*owned*/, QObject *parent): @@ -81,12 +127,19 @@ Object::Object(QObject *parent): m_d(object_new(this, objectBooleanChanged, objectBytearrayChanged, - objectIntegerChanged, + objectF32Changed, + objectF64Changed, + objectI16Changed, + objectI32Changed, + objectI64Changed, + objectI8Changed, objectOptionalBytearrayChanged, objectOptionalStringChanged, objectStringChanged, + objectU16Changed, + objectU32Changed, objectU64Changed, - objectUintegerChanged)), + objectU8Changed)), m_ownsPrivate(true) { } @@ -112,12 +165,47 @@ QByteArray Object::bytearray() const void Object::setBytearray(const QByteArray& v) { object_bytearray_set(m_d, v.data(), v.size()); } -qint32 Object::integer() const +float Object::f32() const { - return object_integer_get(m_d); + return object_f32_get(m_d); } -void Object::setInteger(qint32 v) { - object_integer_set(m_d, v); +void Object::setF32(float v) { + object_f32_set(m_d, v); +} +double Object::f64() const +{ + return object_f64_get(m_d); +} +void Object::setF64(double v) { + object_f64_set(m_d, v); +} +qint16 Object::i16() const +{ + return object_i16_get(m_d); +} +void Object::setI16(qint16 v) { + object_i16_set(m_d, v); +} +qint32 Object::i32() const +{ + return object_i32_get(m_d); +} +void Object::setI32(qint32 v) { + object_i32_set(m_d, v); +} +qint64 Object::i64() const +{ + return object_i64_get(m_d); +} +void Object::setI64(qint64 v) { + object_i64_set(m_d, v); +} +qint8 Object::i8() const +{ + return object_i8_get(m_d); +} +void Object::setI8(qint8 v) { + object_i8_set(m_d, v); } QByteArray Object::optionalBytearray() const { @@ -154,6 +242,20 @@ QString Object::string() const void Object::setString(const QString& v) { object_string_set(m_d, reinterpret_cast(v.data()), v.size()); } +quint16 Object::u16() const +{ + return object_u16_get(m_d); +} +void Object::setU16(uint v) { + object_u16_set(m_d, v); +} +quint32 Object::u32() const +{ + return object_u32_get(m_d); +} +void Object::setU32(uint v) { + object_u32_set(m_d, v); +} quint64 Object::u64() const { return object_u64_get(m_d); @@ -161,10 +263,10 @@ quint64 Object::u64() const void Object::setU64(quint64 v) { object_u64_set(m_d, v); } -quint32 Object::uinteger() const +quint8 Object::u8() const { - return object_uinteger_get(m_d); + return object_u8_get(m_d); } -void Object::setUinteger(uint v) { - object_uinteger_set(m_d, v); +void Object::setU8(quint8 v) { + object_u8_set(m_d, v); } diff --git a/tests/test_object_types_rust.h b/tests/test_object_types_rust.h index 95aeb59..18bf7c0 100644 --- a/tests/test_object_types_rust.h +++ b/tests/test_object_types_rust.h @@ -17,12 +17,19 @@ private: bool m_ownsPrivate; Q_PROPERTY(bool boolean READ boolean WRITE setBoolean NOTIFY booleanChanged FINAL) Q_PROPERTY(QByteArray bytearray READ bytearray WRITE setBytearray NOTIFY bytearrayChanged FINAL) - Q_PROPERTY(qint32 integer READ integer WRITE setInteger NOTIFY integerChanged FINAL) + Q_PROPERTY(float f32 READ f32 WRITE setF32 NOTIFY f32Changed FINAL) + Q_PROPERTY(double f64 READ f64 WRITE setF64 NOTIFY f64Changed FINAL) + Q_PROPERTY(qint16 i16 READ i16 WRITE setI16 NOTIFY i16Changed FINAL) + Q_PROPERTY(qint32 i32 READ i32 WRITE setI32 NOTIFY i32Changed FINAL) + Q_PROPERTY(qint64 i64 READ i64 WRITE setI64 NOTIFY i64Changed FINAL) + Q_PROPERTY(qint8 i8 READ i8 WRITE setI8 NOTIFY i8Changed FINAL) Q_PROPERTY(QByteArray optionalBytearray READ optionalBytearray WRITE setOptionalBytearray NOTIFY optionalBytearrayChanged FINAL) Q_PROPERTY(QString optionalString READ optionalString WRITE setOptionalString NOTIFY optionalStringChanged FINAL) Q_PROPERTY(QString string READ string WRITE setString NOTIFY stringChanged FINAL) + Q_PROPERTY(quint16 u16 READ u16 WRITE setU16 NOTIFY u16Changed FINAL) + Q_PROPERTY(quint32 u32 READ u32 WRITE setU32 NOTIFY u32Changed FINAL) Q_PROPERTY(quint64 u64 READ u64 WRITE setU64 NOTIFY u64Changed FINAL) - Q_PROPERTY(quint32 uinteger READ uinteger WRITE setUinteger NOTIFY uintegerChanged FINAL) + Q_PROPERTY(quint8 u8 READ u8 WRITE setU8 NOTIFY u8Changed FINAL) explicit Object(bool owned, QObject *parent); public: explicit Object(QObject *parent = nullptr); @@ -31,26 +38,47 @@ public: void setBoolean(bool v); QByteArray bytearray() const; void setBytearray(const QByteArray& v); - qint32 integer() const; - void setInteger(qint32 v); + float f32() const; + void setF32(float v); + double f64() const; + void setF64(double v); + qint16 i16() const; + void setI16(qint16 v); + qint32 i32() const; + void setI32(qint32 v); + qint64 i64() const; + void setI64(qint64 v); + qint8 i8() const; + void setI8(qint8 v); QByteArray optionalBytearray() const; void setOptionalBytearray(const QByteArray& v); QString optionalString() const; void setOptionalString(const QString& v); QString string() const; void setString(const QString& v); + quint16 u16() const; + void setU16(uint v); + quint32 u32() const; + void setU32(uint v); quint64 u64() const; void setU64(quint64 v); - quint32 uinteger() const; - void setUinteger(uint v); + quint8 u8() const; + void setU8(quint8 v); signals: void booleanChanged(); void bytearrayChanged(); - void integerChanged(); + void f32Changed(); + void f64Changed(); + void i16Changed(); + void i32Changed(); + void i64Changed(); + void i8Changed(); void optionalBytearrayChanged(); void optionalStringChanged(); void stringChanged(); + void u16Changed(); + void u32Changed(); void u64Changed(); - void uintegerChanged(); + void u8Changed(); }; #endif // TEST_OBJECT_TYPES_RUST_H diff --git a/tests/test_tree_rust.cpp b/tests/test_tree_rust.cpp index a823173..8731b95 100644 --- a/tests/test_tree_rust.cpp +++ b/tests/test_tree_rust.cpp @@ -127,10 +127,10 @@ QVariant Persons::userName(const QModelIndex& index) const bool Persons::setUserName(const QModelIndex& index, const QVariant& value) { + bool set = false; if (!value.canConvert(qMetaTypeId())) { return false; } - bool set = false; const QString s = value.value(); set = persons_set_data_user_name(m_d, index.internalId(), s.utf16(), s.length()); if (set) {