Skip to content

Commit

Permalink
Test modules (#464)
Browse files Browse the repository at this point in the history
* Allocator test module

* More transitioning to modules

* Final op_utils tetss

* run_program

* test_ops

* traverse_path

* Remainder
  • Loading branch information
Rigidity authored Sep 19, 2024
1 parent 3291a32 commit e5eef98
Show file tree
Hide file tree
Showing 17 changed files with 4,051 additions and 4,068 deletions.
2,119 changes: 1,042 additions & 1,077 deletions src/allocator.rs

Large diffs are not rendered by default.

70 changes: 37 additions & 33 deletions src/more_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,43 +982,47 @@ pub fn op_modpow(a: &mut Allocator, input: NodePtr, max_cost: Cost) -> Response
}

#[cfg(test)]
fn test_sha256_atom(buf: &[u8]) {
let mut a = Allocator::new();
let mut args = a.nil();
let v = a.new_atom(buf).unwrap();
args = a.new_pair(v, args).unwrap();
let v = a.new_small_number(1).unwrap();
args = a.new_pair(v, args).unwrap();

let cost = SHA256_BASE_COST
+ (2 * SHA256_COST_PER_ARG)
+ ((1 + buf.len()) as Cost * SHA256_COST_PER_BYTE)
+ 32 * MALLOC_COST_PER_BYTE;
let Reduction(actual_cost, result) = op_sha256(&mut a, args, cost).unwrap();
mod tests {
use super::*;

fn test_sha256_atom(buf: &[u8]) {
let mut a = Allocator::new();
let mut args = a.nil();
let v = a.new_atom(buf).unwrap();
args = a.new_pair(v, args).unwrap();
let v = a.new_small_number(1).unwrap();
args = a.new_pair(v, args).unwrap();

let cost = SHA256_BASE_COST
+ (2 * SHA256_COST_PER_ARG)
+ ((1 + buf.len()) as Cost * SHA256_COST_PER_BYTE)
+ 32 * MALLOC_COST_PER_BYTE;
let Reduction(actual_cost, result) = op_sha256(&mut a, args, cost).unwrap();

let mut hasher = Sha256::new();
hasher.update([1_u8]);
if !buf.is_empty() {
hasher.update(buf);
}

let mut hasher = Sha256::new();
hasher.update([1_u8]);
if !buf.is_empty() {
hasher.update(buf);
println!("buf: {buf:?}");
assert_eq!(a.atom(result).as_ref(), hasher.finalize().as_slice());
assert_eq!(actual_cost, cost);
}

println!("buf: {buf:?}");
assert_eq!(a.atom(result).as_ref(), hasher.finalize().as_slice());
assert_eq!(actual_cost, cost);
}

#[test]
fn sha256_small_values() {
test_sha256_atom(&[]);
for val in 0..255 {
test_sha256_atom(&[val]);
}
#[test]
fn sha256_small_values() {
test_sha256_atom(&[]);
for val in 0..255 {
test_sha256_atom(&[val]);
}

for val in 0..255 {
test_sha256_atom(&[0, val]);
}
for val in 0..255 {
test_sha256_atom(&[0, val]);
}

for val in 0..255 {
test_sha256_atom(&[0xff, val]);
for val in 0..255 {
test_sha256_atom(&[0xff, val]);
}
}
}
Loading

0 comments on commit e5eef98

Please sign in to comment.