Skip to content

Commit

Permalink
fix: ton vendor code test
Browse files Browse the repository at this point in the history
  • Loading branch information
soralit committed Oct 10, 2024
1 parent 51b0530 commit 213b2bd
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 326 deletions.
42 changes: 0 additions & 42 deletions rust/apps/ton/src/vendor/cell/bit_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,3 @@ fn create_biguint_with_ones(n: usize) -> BigUint {
BigUint::from_bytes_be(&msb)
}

#[cfg(test)]
mod tests {
use num_bigint::BigUint;
use num_traits::ToPrimitive;

use crate::cell::bit_string::create_biguint_with_ones;

#[test]
fn test_create_biguint_with_ones() -> anyhow::Result<()> {
let r = create_biguint_with_ones(17);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x1ffffu32));
let r = create_biguint_with_ones(16);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0xffffu32));
let r = create_biguint_with_ones(15);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x7fffu32));
let r = create_biguint_with_ones(13);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x1fffu32));
let r = create_biguint_with_ones(11);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x07ffu32));
let r = create_biguint_with_ones(9);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x01ffu32));
let r = create_biguint_with_ones(8);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x0ffu32));
let r = create_biguint_with_ones(7);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x07fu32));
let r = create_biguint_with_ones(1);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x01u32));
let r = create_biguint_with_ones(0);
println!("{:08x}", r.to_u32().unwrap());
assert_eq!(r, BigUint::from(0x00u32));
Ok(())
}
}
208 changes: 0 additions & 208 deletions rust/apps/ton/src/vendor/cell/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,211 +323,3 @@ impl Default for CellBuilder {
Self::new()
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use num_bigint::{BigInt, BigUint, Sign};
use tokio_test::{assert_err, assert_ok};

use crate::address::TonAddress;
use crate::cell::builder::extend_and_invert_bits;
use crate::cell::CellBuilder;

#[test]
fn test_extend_and_invert_bits() -> anyhow::Result<()> {
let a = BigUint::from(1u8);
let b = extend_and_invert_bits(8, &a)?;
println!("a: {:0x}", a);
println!("b: {:0x}", b);
assert_eq!(b, BigUint::from(0xffu8));

let b = extend_and_invert_bits(16, &a)?;
assert_eq!(b, BigUint::from_slice(&[0xffffu32]));

let b = extend_and_invert_bits(20, &a)?;
assert_eq!(b, BigUint::from_slice(&[0xfffffu32]));

let b = extend_and_invert_bits(8, &a)?;
assert_eq!(b, BigUint::from_slice(&[0xffu32]));

let b = extend_and_invert_bits(9, &a)?;
assert_eq!(b, BigUint::from_slice(&[0x1ffu32]));

assert_err!(extend_and_invert_bits(3, &BigUint::from(10u32)));
Ok(())
}

#[test]
fn write_bit() -> anyhow::Result<()> {
let mut writer = CellBuilder::new();
let cell = writer.store_bit(true)?.build()?;
assert_eq!(cell.data, [0b1000_0000]);
assert_eq!(cell.bit_len, 1);
let mut reader = cell.parser();
let result = reader.load_bit()?;
assert!(result);
Ok(())
}

#[test]
fn write_u8() -> anyhow::Result<()> {
let value = 234u8;
let mut writer = CellBuilder::new();
let cell = writer.store_u8(8, value)?.build()?;
assert_eq!(cell.data, [0b1110_1010]);
assert_eq!(cell.bit_len, 8);
let mut reader = cell.parser();
let result = reader.load_u8(8)?;
assert_eq!(result, value);
Ok(())
}

#[test]
fn write_u32() -> anyhow::Result<()> {
let value = 0xFAD45AADu32;
let mut writer = CellBuilder::new();
let cell = writer.store_u32(32, value)?.build()?;
assert_eq!(cell.data, [0xFA, 0xD4, 0x5A, 0xAD]);
assert_eq!(cell.bit_len, 32);
let mut reader = cell.parser();
let result = reader.load_u32(32)?;
assert_eq!(result, value);
Ok(())
}

#[test]
fn write_u64() -> anyhow::Result<()> {
let value = 0xFAD45AADAA12FF45;
let mut writer = CellBuilder::new();
let cell = writer.store_u64(64, value)?.build()?;
assert_eq!(cell.data, [0xFA, 0xD4, 0x5A, 0xAD, 0xAA, 0x12, 0xFF, 0x45]);
assert_eq!(cell.bit_len, 64);
let mut reader = cell.parser();
let result = reader.load_u64(64)?;
assert_eq!(result, value);
Ok(())
}

#[test]
fn write_slice() -> anyhow::Result<()> {
let value = [0xFA, 0xD4, 0x5A, 0xAD, 0xAA, 0x12, 0xFF, 0x45];
let mut writer = CellBuilder::new();
let cell = writer.store_slice(&value)?.build()?;
assert_eq!(cell.data, value);
assert_eq!(cell.bit_len, 64);
let mut reader = cell.parser();
let bytes = reader.load_bytes(8)?;
assert_eq!(bytes, value);
Ok(())
}

#[test]
fn write_str() -> anyhow::Result<()> {
let texts = ["hello", "Русский текст", "中华人民共和国", "\u{263A}😃"];
for text in texts {
let mut writer = CellBuilder::new();
let cell = writer.store_string(text)?.build()?;
let text_bytes = text.as_bytes();
assert_eq!(cell.data, text_bytes);
assert_eq!(cell.bit_len, text_bytes.len() * 8);
let mut reader = cell.parser();
let remaining_bytes = reader.remaining_bytes();
let result = reader.load_utf8(remaining_bytes)?;
assert_eq!(result, text);
}
Ok(())
}

#[test]
fn write_address() -> anyhow::Result<()> {
let addr = TonAddress::from_base64_url("EQDk2VTvn04SUKJrW7rXahzdF8_Qi6utb0wj43InCu9vdjrR")?;

let mut writer = CellBuilder::new();
let cell = writer.store_address(&addr)?.build()?;
assert_eq!(
cell.data,
[
128, 28, 155, 42, 157, 243, 233, 194, 74, 20, 77, 107, 119, 90, 237, 67, 155, 162,
249, 250, 17, 117, 117, 173, 233, 132, 124, 110, 68, 225, 93, 237, 238, 192
]
);
assert_eq!(cell.bit_len, 2 + 1 + 8 + 32 * 8);
let mut reader = cell.parser();
let result = reader.load_address()?;
assert_eq!(result, addr);
Ok(())
}

#[test]
fn write_big_int() -> anyhow::Result<()> {
let value = BigInt::from_str("3")?;
let mut writer = CellBuilder::new();
assert_ok!(writer.store_int(33, &value));
let cell = writer.build()?;
println!("cell: {:?}", cell);
let written = BigInt::from_bytes_be(Sign::Plus, &cell.data);
assert_eq!(written, value);

// 256 bits (+ sign)
let value = BigInt::from_str(
"97887266651548624282413032824435501549503168134499591480902563623927645013201",
)?;
let mut writer = CellBuilder::new();
assert_ok!(writer.store_int(257, &value));
let cell = writer.build()?;
println!("cell: {:?}", cell);
let written = BigInt::from_bytes_be(Sign::Plus, &cell.data);
assert_eq!(written, value);

let value = BigInt::from_str("-5")?;
let mut writer = CellBuilder::new();
assert_ok!(writer.store_int(5, &value));
let cell = writer.build()?;
println!("cell: {:?}", cell);
let written = BigInt::from_bytes_be(Sign::Plus, &cell.data[1..]);
let expected = BigInt::from_bytes_be(Sign::Plus, &[0xB0u8]);
assert_eq!(written, expected);
Ok(())
}

#[test]
fn write_load_big_uint() -> anyhow::Result<()> {
let value = BigUint::from_str("3")?;
let mut writer = CellBuilder::new();
assert!(writer.store_uint(1, &value).is_err());
let bits_for_tests = [256, 128, 64, 8];

for bits_num in bits_for_tests.iter() {
assert_ok!(writer.store_uint(*bits_num, &value));
}
let cell = writer.build()?;
println!("cell: {:?}", cell);
let mut cell_parser = cell.parser();
for bits_num in bits_for_tests.iter() {
let written_value = assert_ok!(cell_parser.load_uint(*bits_num));
assert_eq!(written_value, value);
}

// 256 bit
let value = BigUint::from_str(
"97887266651548624282413032824435501549503168134499591480902563623927645013201",
)?;
let mut writer = CellBuilder::new();
assert!(writer.store_uint(255, &value).is_err());
let bits_for_tests = [496, 264, 256];
for bits_num in bits_for_tests.iter() {
assert_ok!(writer.store_uint(*bits_num, &value));
}
let cell = writer.build()?;
let mut cell_parser = cell.parser();
println!("cell: {:?}", cell);
for bits_num in bits_for_tests.iter() {
let written_value = assert_ok!(cell_parser.load_uint(*bits_num));
assert_eq!(written_value, value);
}

Ok(())
}
}
16 changes: 0 additions & 16 deletions rust/apps/ton/src/vendor/cell/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,3 @@ fn read_var_size(
Ok(result)
}

#[cfg(test)]
mod tests {
use tokio_test::assert_ok;

use super::*;

#[test]
fn test_raw_cell_serialize() {
let raw_cell = RawCell::new(vec![1; 128], 1023, vec![], 255, false);
let raw_bag = RawBagOfCells {
cells: vec![raw_cell],
roots: vec![0],
};
let _res = assert_ok!(raw_bag.serialize(false));
}
}
60 changes: 0 additions & 60 deletions rust/apps/ton/src/vendor/cell/state_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,63 +63,3 @@ impl StateInit {
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use super::StateInitBuilder;
use crate::cell::CellBuilder;

#[test]
fn test_state_init() -> anyhow::Result<()> {
let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
let state_init = StateInitBuilder::new(&code, &data)
.with_split_depth(true)
.with_tick_tock(true)
.with_library(true)
.build()?;

assert_eq!(state_init.data[0], 0b11111000);
println!("{:08b}", state_init.data[0]);

let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
let state_init = StateInitBuilder::new(&code, &data)
.with_split_depth(false)
.with_tick_tock(false)
.with_library(false)
.build()?;

assert_eq!(state_init.data[0], 0b00110000);

let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
let state_init = StateInitBuilder::new(&code, &data)
.with_split_depth(true)
.with_tick_tock(false)
.with_library(false)
.build()?;

assert_eq!(state_init.data[0], 0b10110000);

let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
let state_init = StateInitBuilder::new(&code, &data)
.with_split_depth(false)
.with_tick_tock(true)
.with_library(false)
.build()?;
assert_eq!(state_init.data[0], 0b01110000);

let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
let state_init = StateInitBuilder::new(&code, &data)
.with_split_depth(false)
.with_tick_tock(false)
.with_library(true)
.build()?;
assert_eq!(state_init.data[0], 0b00111000);
Ok(())
}
}

0 comments on commit 213b2bd

Please sign in to comment.