Browse Source

test: rust codec ffi test framework

master
Dnomd343 2 years ago
parent
commit
f730407978
  1. 2
      src/cli/src/main.rs
  2. 23
      src/rust_ffi/src/codec/ffi.rs
  3. 3
      src/rust_ffi/src/codec/mod.rs
  4. 71
      src/rust_ffi/src/codec/tests.rs
  5. 4
      src/rust_ffi/src/lib.rs
  6. 21
      src/rust_ffi/src/metadata.rs
  7. 4
      test/ffi/codec.cc

2
src/cli/src/main.rs

@ -1,7 +1,7 @@
extern crate klotski_ffi;
fn main() {
// println!("{:#?}", klotski_ffi::load_metadata());
println!("{:#?}", klotski_ffi::metadata());
// println!("{}", klotski_ffi::is_short_code_available());
// println!("{}", klotski_ffi::is_short_code_available_fast());

23
src/rust_ffi/src/codec/ffi.rs

@ -1,43 +1,50 @@
use std::ffi::{c_char, CString};
use super::Core;
use std::ffi::{c_char, CString};
fn short_code_enable() {
#[allow(dead_code)]
pub fn short_code_enable() {
unsafe {
Core::short_code_enable()
}
}
fn short_code_enable_fast() {
#[allow(dead_code)]
pub fn short_code_enable_fast() {
unsafe {
Core::short_code_enable_fast()
}
}
fn is_short_code_available() -> bool {
#[allow(dead_code)]
pub fn is_short_code_available() -> bool {
unsafe {
Core::is_short_code_available()
}
}
fn is_short_code_available_fast() -> bool {
#[allow(dead_code)]
pub fn is_short_code_available_fast() -> bool {
unsafe {
Core::is_short_code_available_fast()
}
}
fn raw_code_check(raw_code: u64) -> bool {
#[allow(dead_code)]
pub fn raw_code_check(raw_code: u64) -> bool {
unsafe {
Core::raw_code_check(raw_code)
}
}
fn short_code_check(short_code: u32) -> bool {
#[allow(dead_code)]
pub fn short_code_check(short_code: u32) -> bool {
unsafe {
Core::short_code_check(short_code)
}
}
fn common_code_check(common_code: u64) -> bool {
#[allow(dead_code)]
pub fn common_code_check(common_code: u64) -> bool {
unsafe {
Core::common_code_check(common_code)
}

3
src/rust_ffi/src/codec/mod.rs

@ -1,3 +1,4 @@
pub mod ffi;
mod ffi;
mod tests;
use super::core::Core;

71
src/rust_ffi/src/codec/tests.rs

@ -0,0 +1,71 @@
mod ffi {
#[allow(unused_imports)]
use crate::codec::ffi as codec_ffi;
#[test]
fn warm_up() {
assert_eq!(codec_ffi::is_short_code_available(), false);
codec_ffi::short_code_enable();
assert_eq!(codec_ffi::is_short_code_available(), true);
assert_eq!(codec_ffi::is_short_code_available_fast(), false);
codec_ffi::short_code_enable_fast();
assert_eq!(codec_ffi::is_short_code_available_fast(), true);
}
#[allow(dead_code)]
const TEST_SHORT_CODE_OK: u32 = 4091296;
#[allow(dead_code)]
const TEST_SHORT_CODE_STR_OK: &str = "4WVE1";
#[allow(dead_code)]
const TEST_COMMON_CODE_OK: u64 = 0x1_A9BF_0C00;
#[allow(dead_code)]
const TEST_COMMON_CODE_STR_OK: &str = "1A9BF0C00";
#[allow(dead_code)]
const TEST_COMMON_CODE_STR_SHR_OK: &str = "1A9BF0C";
#[allow(dead_code)]
const TEST_RAW_CODE_OK: u64 = 0x0603_EDF5_CAFF_F5E2;
#[allow(dead_code)]
const TEST_SHORT_CODE_ERR: u32 = 29670987;
#[allow(dead_code)]
const TEST_COMMON_CODE_ERR: u64 = 0x1_2190_2300;
#[allow(dead_code)]
const TEST_RAW_CODE_ERR: u64 = 0x0A34_182B_3810_2D21;
#[allow(dead_code)]
const TEST_SHORT_CODE_STR_ERR: &str = "R50EH";
#[allow(dead_code)]
const TEST_COMMON_CODE_STR_ERR: &str = "123J432A9";
#[test]
fn checker() {
assert_eq!(codec_ffi::raw_code_check(TEST_RAW_CODE_OK), true);
assert_eq!(codec_ffi::raw_code_check(TEST_RAW_CODE_ERR), false);
assert_eq!(codec_ffi::short_code_check(TEST_SHORT_CODE_OK), true);
assert_eq!(codec_ffi::short_code_check(TEST_SHORT_CODE_ERR), false);
assert_eq!(codec_ffi::common_code_check(TEST_COMMON_CODE_OK), true);
assert_eq!(codec_ffi::common_code_check(TEST_COMMON_CODE_ERR), false);
}
// TODO: string
// TODO: convert
// TODO: convert_unsafe
// TODO: mirror
// TODO: mirror_unsafe
}

4
src/rust_ffi/src/lib.rs

@ -2,6 +2,4 @@ mod core;
mod codec;
mod metadata;
pub use metadata::load_metadata;
pub use codec::ffi::*;
pub use metadata::load_metadata as metadata;

21
src/rust_ffi/src/metadata.rs

@ -1,4 +1,4 @@
use super::core::Core;
use crate::core::Core;
use Core::CStringDump;
#[derive(Debug)]
@ -35,3 +35,22 @@ pub fn load_metadata() -> Metadata {
}
}
}
#[test]
fn tests() {
let metadata = load_metadata();
assert_ne!(metadata.author, "");
assert_ne!(metadata.system, "");
assert_ne!(metadata.git_tag, "");
assert_ne!(metadata.version, "");
assert_ne!(metadata.compiler, "");
assert_ne!(metadata.commit_id, "");
assert_ne!(metadata.build_time, "");
assert_ne!(metadata.git_branch, "");
assert_ne!(metadata.project_url, "");
assert!(metadata.version_patch < 65536);
assert!(metadata.version_minor < 65536);
assert!(metadata.version_patch < 65536);
}

4
test/ffi/codec.cc

@ -151,7 +151,6 @@ TEST(FFI, codec_mirror) {
bool result;
uint64_t raw_code;
/// test of safe version
EXPECT_EQ(is_vertical_mirror(TEST_RAW_CODE_ERR, &result), false);
EXPECT_EQ(is_horizontal_mirror(TEST_RAW_CODE_ERR, &result), false);
EXPECT_EQ(to_vertical_mirror(TEST_RAW_CODE_ERR, &raw_code), false);
@ -174,8 +173,9 @@ TEST(FFI, codec_mirror) {
EXPECT_EQ(raw_code, TEST_MIRROR_H2);
EXPECT_EQ(to_horizontal_mirror(TEST_MIRROR_H2, &raw_code), true);
EXPECT_EQ(raw_code, TEST_MIRROR_H1);
}
/// test of unsafe version
TEST(FFI, codec_mirror_unsafe) {
EXPECT_EQ(is_vertical_mirror_unsafe(TEST_MIRROR_V), true);
EXPECT_EQ(is_vertical_mirror_unsafe(TEST_MIRROR_V1), false);
EXPECT_EQ(is_vertical_mirror_unsafe(TEST_MIRROR_V2), false);

Loading…
Cancel
Save