Skip to content

Commit

Permalink
[+] v0.1.4-pre
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMaka committed Sep 30, 2024
1 parent da223ae commit 006201e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 61 deletions.
2 changes: 2 additions & 0 deletions misc/kawalas_log.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

##### v.1.4-pre
- Added `chunk32` and `chunks32` to kwl32
- Added `PartialEq` to `Bytes` and `Word`
- Added `remove` and `__remove` to `View`

##### v0.1.3
- Added edge case handlers to `pad32l` and `pad32r`
Expand Down
9 changes: 7 additions & 2 deletions misc/kawalas_todo.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

## Current to do

- [ ] Masks => View methods for simplified masking
- We can already build a mask, but can make the process feel more intuitive

- [ ] Replace => View method to replace from word, consuming replacement
- There is already replace, and long winded ways to do above, just add an abstraction

- [ ] Deque => View method to pop from the top of the page
- Not an issue just undecided on how I want to handle empty, if at all

- [ ] Remove => View method to remove an element from within the middle of the page
- [x] Remove => View method to remove an element from within the middle of the page
- Not something I'd use over clearing, but very reasonable to expect to have the option

- [x] Chunk => kwl32 dedicated chunk function
Expand Down
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ impl<I: std::slice::SliceIndex<[u8]>> std::ops::Index<I> for Bytes {
}
}

impl PartialEq for Bytes {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
( Bytes::Bytes4(x) , Bytes::Bytes4(y) ) => x == y,
( Bytes::Bytes32(x), Bytes::Bytes32(y)) => x == y,
( Bytes::Array(x) , Bytes::Array(y) ) => x == y,
_ => false
}
}
}

/* ----------------------------------------------------------------------------
Calldata structure
-----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -196,6 +207,13 @@ impl Word {
pub fn hex_0x(&self) -> String {
"0x".to_owned() + &self.data.hex()
}

}

impl PartialEq for Word {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}

/* ----------------------------------------------------------------------------
Expand Down Expand Up @@ -416,7 +434,15 @@ View cont.. exposed functions that take or return Kawala types
pub fn __pop(&mut self) -> Word {
self.page.pop() . unwrap_or(Word::from_bytes(&EMPTY_BYTES32))
}

// remove an element
pub fn __remove(&mut self, index : usize) -> Word {
Some(self.page.remove(index)) . unwrap_or(Word::from_bytes(&EMPTY_BYTES32))
}

/*
To do: Profile pseudo deque vs `VecDeque` refactor. Consider how infrequent.
*/

/* -----------------------*NOTE*: end of destructive ------------------------ */

// returns a ref to all 32 byte Words
Expand Down
110 changes: 53 additions & 57 deletions tests/kwl32_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ mod kwl32_util {
. into_iter()
. chain([1u8; 16])
. collect::<Vec<u8>>();

assert_eq!(util::roll32r(&input, 16), expected . as_slice());
}

Expand All @@ -117,7 +116,6 @@ mod kwl32_util {
. into_iter()
. chain([0u8; 16])
. collect::<Vec<u8>>();

assert_eq!(util::roll32l(&input, 16), expected . as_slice());
}

Expand All @@ -127,77 +125,75 @@ mod kwl32_util {
fn roll32r_shift_exceeding_32() {
let input = [1u8; 32];
let expected = input;

assert_eq!(util::roll32r(&input, 64), expected);
}

#[test]
fn roll32l_shift_exceeding_32() {
let input = [1u8; 32];
let expected = input;

assert_eq!(util::roll32l(&input, 64), expected);
}

//-------- -------- CHUNK 32 -------- --------//

// less input
#[test]
fn chunk32_less_than_32_bytes() {
let input = [0u8; 16];
// less input
#[test]
fn chunk32_less_than_32_bytes() {
let input = [0u8; 16];
let expected = util::pad32r(&input);
assert_eq!(util::chunk32(&input), expected);
}
}

// exact input
#[test]
fn chunk32_exact_32_bytes() {
let input = [0u8; 32];
// exact input
#[test]
fn chunk32_exact_32_bytes() {
let input = [0u8; 32];
assert_eq!(util::chunk32(&input), input);
}

// more input
#[test]
fn chunk32_more_than_32_bytes() {
let input = [0u8; 64];
let expected = [0u8; 32];
assert_eq!(util::chunk32(&input), expected);
}

//-------- -------- CHUNK 32 -------- --------//

// empty input
#[test]
fn chunks32_empty() { assert_eq!(util::chunks32(&[]), Vec::<[u8;32]>::new()) }

// exact input
#[test]
fn chunks32_exact_32_bytes() {
let input = [0u8; 32];
assert_eq!(util::chunks32(&input), vec![input]);
}

// correct multi input
#[test]
fn chunks32_multiple_32_bytes() {
let input = [0u8; 96];
assert_eq!(util::chunks32(&input), vec![
[0u8;32],
[0u8;32],
[0u8;32]
]);
}

// irregular multi (will trunk)
#[test]
fn chunks32_not_multiple_32_bytes() {
let input = [0u8; 65];
assert_eq!(util::chunks32(&input), vec![
[0u8;32],
[0u8;32],
[0u8;32]
]);
}
}
// more input
#[test]
fn chunk32_more_than_32_bytes() {
let input = [0u8; 64];
let expected = [0u8; 32];
assert_eq!(util::chunk32(&input), expected);
}
//-------- -------- CHUNKS 32 -------- --------//
// empty input
#[test]
fn chunks32_empty() { assert_eq!(util::chunks32(&[]), Vec::<[u8;32]>::new()) }
// exact input
#[test]
fn chunks32_exact_32_bytes() {
let input = [0u8; 32];
assert_eq!(util::chunks32(&input), vec![input]);
}
// regular multi input
#[test]
fn chunks32_multiple_32_bytes() {
let input = [0u8; 96];
assert_eq!(util::chunks32(&input), vec![
[0u8;32],
[0u8;32],
[0u8;32]
]);
}
// irregular multi (will trunk)
#[test]
fn chunks32_not_multiple_32_bytes() {
let input = [0u8; 65];
assert_eq!(util::chunks32(&input), vec![
[0u8;32],
[0u8;32],
[0u8;32]
]);
}

//-------- -------- XOR32 -------- --------//

Expand Down
15 changes: 14 additions & 1 deletion tests/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod view {

use kawala::{ View, Calldata, WithSig };
use kawala::{ View, Calldata, WithSig, Word };
#[test]
fn view() -> (){

Expand Down Expand Up @@ -105,5 +105,18 @@ mod view {
view.replace(0,word2);
assert_eq!(view.word(0), word2);
}


#[test]
fn __remove_word() -> (){
let mut view = View::new(Calldata::from_bytes(&[[0u8;32],[1u8;32]].concat()), WithSig::False);
let expected = Word::from_bytes(&[1u8;32]);
assert_eq!(view.word_count(), 2);
let input = view.__remove(1);
assert_eq!(input, expected);
assert_eq!(view.word_count(), 1);
}


}

0 comments on commit 006201e

Please sign in to comment.