Skip to content

Commit

Permalink
test,fix: {string,value}_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
abesto committed Jul 14, 2024
1 parent ff6ba32 commit 6ef931b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
4 changes: 3 additions & 1 deletion crates/kernel/src/builtins/bf_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ fn bf_string_hash(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
match bf_args.args[0].variant() {
Variant::Str(s) => {
let hash_digest = md5::Md5::digest(s.as_str().as_bytes());
Ok(Ret(v_str(format!("{:x}", hash_digest).as_str())))
Ok(Ret(v_str(
format!("{:x}", hash_digest).to_uppercase().as_str(),
)))
}
_ => Err(BfErr::Code(E_INVARG)),
}
Expand Down
12 changes: 6 additions & 6 deletions crates/kernel/src/builtins/bf_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
// this program. If not, see <https://www.gnu.org/licenses/>.
//

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use md5::Digest;
use moor_compiler::offset_for_builtin;
use moor_values::var::Error::{E_ARGS, E_INVARG, E_TYPE};
use moor_values::var::Variant;
Expand Down Expand Up @@ -149,9 +147,11 @@ fn bf_value_hash(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
if bf_args.args.len() != 1 {
return Err(BfErr::Code(E_ARGS));
}
let mut s = DefaultHasher::new();
bf_args.args[0].hash(&mut s);
Ok(Ret(v_int(s.finish() as i64)))
let s = bf_args.args[0].to_literal();
let hash_digest = md5::Md5::digest(s.as_bytes());
Ok(Ret(v_str(
format!("{:x}", hash_digest).to_uppercase().as_str(),
)))
}
bf_declare!(value_hash, bf_value_hash);

Expand Down
36 changes: 36 additions & 0 deletions crates/kernel/testsuite/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
This is mostly a port of (part of) the regression suite bundled with Stunt MOO:

https://github.com/toddsundsted/stunt/tree/master/test

| Status | `stunt` test(s) | `moor` test(s) | Notes |
| ------ | ---------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------------------- |
|| `test_algorithms.rb` | `algorithms.moot` | `stunt` added multiple hashing algorithms (not supported in `moor`). Fuzz tests not ported. |
| 🚫 | `test_anonymous.rb` | N/A | `moor` doesn't support anonymous objects. |
|| `test_basic.rb`, `basic/*` | `basic/*.moot` | |
| 🔜 | `test_canned_dbs.rb` | N/A | |
| 🤔 | `test_collection_improvements.rb` | N/A | Are these tests valuable / relevant for `moor`? |
|| `test_create.rb` | `create.moot` | |
|| `test_equality.rb` | `equality.moot` | |
|| `test_eval.rb` | `eval/*.moot` | |
| 🚫 | `test_exec.rb` | N/A | `moor` doesn't support this Stunt extension. |
| 🚫 | `test_fileio.rb` | N/A | `moor` doesn't support this Stunt extension. |
| 🚫 | `test_garbage_collection.rb` | N/A | `moor` doesn't support this Stunt extension. |
| 🚫 | `test_http.rb` | N/A | `moor` doesn't support this Stunt extension. |
|| `test_huh.rb` | `huh.moot` | See also `huh` test in the `telnet-host` crate. |
| 🚫 | `test_index_and_range_extensions.rb` | N/A | `moor` doesn't support this Stunt extension. |
| 🚫 | `test_json.rb` | N/A | `moor` doesn't support this Stunt extension. |
| 🔜 | `test_limits.rb` | N/A | |
|| `test_looping.rb` | `looping.moot` | |
| 🚫 | `test_map.rb` | N/A | `moor` doesn't support this Stunt extension. |
|| `test_math.rb` | `math.moot` | |
| 🔜 | `test_miscellaneous.rb` | N/A | |
| 🔜 | `test_moocode_parsing.rb` | N/A | |
| 🔜 | `test_objects.rb` | N/A | |
| 🔜 | `test_objects_and_properties.rb` | N/A | |
| 🔜 | `test_objects_and_verbs.rb` | N/A | |
| 🔜 | `test_primitives.rb` | N/A | |
|| `test_recycle.rb` | `recycle.moot` | |
| 🔜 | `test_stress_objects.rb` | N/A | |
|| `test_string_operations.rb` | `string_operations.moot` | Extended with cases based on LambdaMOO Programmer's Manual |
| 🚫 | `test_switch_player.rb` | N/A | `moor` doesn't support this Stunt extension. |
| 🚫 | `test_system_builtins.rb` | N/A | `moor` doesn't support this Stunt extension (`getenv`). |
| 🔜 | `test_task_local.rb` | N/A | |
| 🔜 | `test_things_that_used_to_crash_the_server.rb` | N/A | Probably useful to test these, since they were tricky for another server at somepoint |
| 🚫 | `test_verb_cache.rb` | N/A | `moor` doesn't support this Stunt extension. |
25 changes: 25 additions & 0 deletions crates/kernel/testsuite/moot/algorithms.moot
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// test_that_hashing_works
@programmer

; return string_hash("abc");
"900150983CD24FB0D6963F7D28E17F72"

; return string_hash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
"8215EF0796A20BCAAAE116D3876C664A"

// TODO: binary_hash is not implemented
// ; return binary_hash("~00~00~00~00~00");
// "8855508AADE16EC573D21E6A485DFD0A7624085C1A14B5ECDD6485DE0C6839A4"

// TODO: binary_hash is not implemented
// ; return binary_hash("~A7~CE~44~8E~D2~4C~16~61~F6~F2~01~55~5A~32~BC~B0~29~EC~02~86~76~CD~9B~05~E6~36~F3~2E~76~78~3C~F0~D1~45~40~C7~DA~F3~C7~C0~AA~43~1E~D2~D0~03~5D~21~F7~0C~C9~19~F0~82~67~76~E4~19~3A~02~F0~7E~F8~BE~CC~6A~27~46~C2~C3~B8~1C~91~1F~7E~F7~F5~50~D9~0E~D8~D6~89~DA~86~B5~95~E0~66~0C~4A~92~01~2B~8A~AE~2E~7F~3B~88"); // "A088FB0606E595E8A248393A296BE79C5A0FE7417FE267334E2113D6613B8D70"

; return value_hash({});
"99914B932BD37A50B983C5E7C90AE93B"

; $tmp = {1, 2, 3, {"fee", "fi", "fo", "fum"}};
; return value_hash($tmp);
"4DF7F9A6A00994056746D3DA490249EA"

; return string_hash(toliteral($tmp));
"4DF7F9A6A00994056746D3DA490249EA"
2 changes: 1 addition & 1 deletion crates/kernel/testsuite/moot_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@ fn test(db: Box<dyn Database>, path: &Path) {
fn test_single() {
// cargo test -p moor-kernel --test moot-suite test_single -- --ignored
// CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --test moot-suite -- test_single --ignored
test_wiredtiger(&testsuite_dir().join("moot/single.moot"));
test_wiredtiger(&testsuite_dir().join("moot/algorithms.moot"));
}
8 changes: 7 additions & 1 deletion crates/moot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ impl<R: MootRunner> MootState<R> {
runner.none()
};

assert_eq!(actual, expected, "Line {line_no}");
// Send the values through the debug formatter, because MOO string comparison
// is case-insensitive, but we want case-sensitive comparison in tests.
assert_eq!(
format!("{actual:?}"),
format!("{expected:?}"),
"Line {line_no}"
);
Ok(())
}

Expand Down

0 comments on commit 6ef931b

Please sign in to comment.