Skip to content

Commit

Permalink
Moves tests into the files where they belong.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Oct 1, 2023
1 parent 07c09fa commit e8d8d17
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
13 changes: 13 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,16 @@ impl<T, E> From<Result<T, E>> for StableResult<T, E> {

/// Return value of programs and syscalls
pub type ProgramResult = StableResult<u64, EbpfError>;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_program_result_is_stable() {
let ok = ProgramResult::Ok(42);
assert_eq!(ok.discriminant(), 0);
let err = ProgramResult::Err(EbpfError::JitNotCompiled);
assert_eq!(err.discriminant(), 1);
}
}
36 changes: 36 additions & 0 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,39 @@ macro_rules! declare_builtin_function {
}
};
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{program::BuiltinFunction, syscalls, vm::TestContextObject};

#[test]
fn test_builtin_program_eq() {
let mut function_registry_a =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_a
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
function_registry_a
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
let mut function_registry_b =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_b
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
function_registry_b
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
let mut function_registry_c =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_c
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
let builtin_program_a = BuiltinProgram::new_loader(Config::default(), function_registry_a);
let builtin_program_b = BuiltinProgram::new_loader(Config::default(), function_registry_b);
assert_eq!(builtin_program_a, builtin_program_b);
let builtin_program_c = BuiltinProgram::new_loader(Config::default(), function_registry_c);
assert_ne!(builtin_program_a, builtin_program_c);
}
}
44 changes: 0 additions & 44 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,47 +422,3 @@ impl<'a, C: ContextObject> EbpfVm<'a, C> {
(instruction_count, result)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{program::BuiltinFunction, syscalls};

#[test]
fn test_program_result_is_stable() {
let ok = ProgramResult::Ok(42);
assert_eq!(ok.discriminant(), 0);
let err = ProgramResult::Err(EbpfError::JitNotCompiled);
assert_eq!(err.discriminant(), 1);
}

#[test]
fn test_builtin_program_eq() {
let mut function_registry_a =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_a
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
function_registry_a
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
let mut function_registry_b =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_b
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
function_registry_b
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
let mut function_registry_c =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_c
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
let builtin_program_a = BuiltinProgram::new_loader(Config::default(), function_registry_a);
let builtin_program_b = BuiltinProgram::new_loader(Config::default(), function_registry_b);
assert_eq!(builtin_program_a, builtin_program_b);
let builtin_program_c = BuiltinProgram::new_loader(Config::default(), function_registry_c);
assert_ne!(builtin_program_a, builtin_program_c);
}
}

0 comments on commit e8d8d17

Please sign in to comment.