Skip to content

Commit

Permalink
Added Test for mozilla::Range::new
Browse files Browse the repository at this point in the history
  • Loading branch information
Redfire75369 committed Dec 2, 2023
1 parent 5344b06 commit 662e50a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
1 change: 0 additions & 1 deletion mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const ENV_VARS: &'static [&'static str] = &[

const EXTRA_FILES: &'static [&'static str] = &[
"makefile.cargo",
"src/rustfmt.toml",
"src/jsglue.hpp",
"src/jsglue.cpp",
];
Expand Down
49 changes: 49 additions & 0 deletions mozjs/tests/range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::ptr;

use mozjs::jsapi::mozilla::Range;
use mozjs::jsapi::{BigIntIsUint64, JS_NewGlobalObject, StringToBigInt, StringToBigInt1};
use mozjs::jsapi::{JSAutoRealm, OnNewGlobalHookOption};
use mozjs::rooted;
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};

#[test]
fn range() {
let engine = JSEngine::init().unwrap();
let runtime = Runtime::new(engine.handle());
let context = runtime.cx();
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = RealmOptions::default();

unsafe {
rooted!(in(context) let global = JS_NewGlobalObject(
context,
&SIMPLE_GLOBAL_CLASS,
ptr::null_mut(),
h_option,
&*c_option,
));
let _ac = JSAutoRealm::new(context, global.get());

// Number.MAX_SAFE_INTEGER + 10
let int = 9007199254741001;
let mut string = int.to_string();
let range = string.as_bytes_mut().as_mut_ptr_range();
let chars = Range::new(range.start, range.end);
rooted!(in(context) let bigint = StringToBigInt(context, chars));
assert!(!bigint.get().is_null());

let mut result = 0;
assert!(BigIntIsUint64(bigint.get(), &mut result));
assert_eq!(result, int);

let mut chars: Vec<_> = string.encode_utf16().collect();
let range = chars.as_mut_ptr_range();
let chars = Range::new(range.start, range.end);
rooted!(in(context) let bigint = StringToBigInt1(context, chars));
assert!(!bigint.get().is_null());

let mut result = 0;
assert!(BigIntIsUint64(bigint.get(), &mut result));
assert_eq!(result, int);
}
}

0 comments on commit 662e50a

Please sign in to comment.