From 863eabc9869c9998f1d1487144b02ec05a59b549 Mon Sep 17 00:00:00 2001 From: Burak Bilge Yalcinkaya Date: Fri, 6 Dec 2024 15:38:36 +0300 Subject: [PATCH] add komet test for object comparison --- .../soroban/contracts/test_compare/Cargo.toml | 15 ++++ .../soroban/contracts/test_compare/src/lib.rs | 69 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/tests/integration/data/soroban/contracts/test_compare/Cargo.toml create mode 100644 src/tests/integration/data/soroban/contracts/test_compare/src/lib.rs diff --git a/src/tests/integration/data/soroban/contracts/test_compare/Cargo.toml b/src/tests/integration/data/soroban/contracts/test_compare/Cargo.toml new file mode 100644 index 0000000..ebf42dd --- /dev/null +++ b/src/tests/integration/data/soroban/contracts/test_compare/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "test_compare" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] +doctest = false + +[dependencies] +soroban-sdk = { workspace = true } + +[dev-dependencies] +soroban-sdk = { workspace = true, features = ["testutils"] } diff --git a/src/tests/integration/data/soroban/contracts/test_compare/src/lib.rs b/src/tests/integration/data/soroban/contracts/test_compare/src/lib.rs new file mode 100644 index 0000000..bbcc351 --- /dev/null +++ b/src/tests/integration/data/soroban/contracts/test_compare/src/lib.rs @@ -0,0 +1,69 @@ +#![no_std] +use core::cmp::Ordering; + +use soroban_sdk::{contract, contractimpl, Address, Env, IntoVal, TryFromVal, Val, Vec}; + +#[contract] +pub struct CompareContract; + +// Compares two values of type `T` using host-side comparison semantics. +// The values are placed into vectors to ensure comparison is performed +// via `obj_cmp`, regardless of the type-specific behavior. +fn compare(e: &Env, a: T, b: T) -> Ordering + where T: TryFromVal + IntoVal +{ + let va = Vec::from_array(&e, [a]); + let vb = Vec::from_array(&e, [b]); + + va.cmp(&vb) +} + +#[contractimpl] +impl CompareContract { + pub fn test_address(_env: Env, a: Address, b: Address) -> bool { + let _ = a.cmp(&b); + true + } + + pub fn test_address_vec(env: Env, a: Address, b: Address) -> bool { + compare(&env, a.clone(), b.clone()) == a.cmp(&b) + } + + /// The `soroban_sdk` implements integer comparison on the Wasm side. This test + /// uses the `compare` function to ensure the integers are compared using the + /// `obj_cmp` host function. It then verifies that the result matches the + /// comparison performed by the SDK's guest-side implementation. + pub fn test_cmp_i128(env: Env, a: i128, b: i128) -> bool { + compare(&env, a, b) == a.cmp(&b) + } + + pub fn test_cmp_i64(env: Env, a: i64, b: i64) -> bool { + compare(&env, a, b) == a.cmp(&b) + } + + pub fn test_cmp_i32(env: Env, a: i32, b: i32) -> bool { + compare(&env, a, b) == a.cmp(&b) + } + + pub fn test_cmp_u128(env: Env, a: i128, b: i128) -> bool { + compare(&env, a, b) == a.cmp(&b) + } + + pub fn test_cmp_u64(env: Env, a: i64, b: i64) -> bool { + compare(&env, a, b) == a.cmp(&b) + } + + pub fn test_cmp_u32(env: Env, a: i32, b: i32) -> bool { + compare(&env, a, b) == a.cmp(&b) + } + + pub fn test_cmp_bool(env: Env, a: bool, b: bool) -> bool { + compare(&env, a, b) == a.cmp(&b) + } + + pub fn test_cmp_void(env: Env) -> bool { + let a = (); + let b = (); + compare(&env, a, b) == a.cmp(&b) + } +}