499 lines
23 KiB
Python
499 lines
23 KiB
Python
"""Runtime tests for the reconstructed as3vm_imp_math translation unit.
|
|
|
|
RED STATE: as3vm_imp_math.c / as3vm_imp_math.h do not exist yet, so setUpClass
|
|
raises "missing as3vm_imp_math runtime inputs". Every assertion below is grounded
|
|
in a specific line of iggy-decompiled/raw/durango/as3vm_imp_math.c (cited inline)
|
|
so a faithful DURANGO reconstruction turns these green.
|
|
|
|
as3vm_imp_math is Iggy's AS3 / AVM2 Math class: abs / ceil / floor / round /
|
|
min / max / pow / sqrt / sin / cos / tan / asin / acos / atan / atan2 / exp /
|
|
log / random, the eight constant getters (E / LN10 / LN2 / LOG10E / LOG2E / PI /
|
|
SQRT1_2 / SQRT2), Math__construct (which just throws) and Number_toString. It
|
|
exports EXACTLY 28 entry points on all four platforms and -- LIKE as3vm_convert /
|
|
as3vm_imp_array but UNLIKE as3vm_gc / as3vm_property -- OWNS NO externally named
|
|
data global.
|
|
|
|
CALL-FRAME MODEL (raw/durango, offsets 64-bit; the runtime test builds ONLY the
|
|
durango dylib so the 64-bit layout is correct):
|
|
- a method thunk receives a four-word call frame: frame[0]=vm, frame[1]=&result
|
|
slot, frame[2]=argc (int32 at +0x10), frame[3]=&this-slot (points at
|
|
[this, arg0, arg1, ...]) (raw:6-34, 733-761).
|
|
- a unary Math method parses its double via as3vm_component_parse_arguments_GC
|
|
(format "f"), computes with libm, boxes via as3vm_make_number_GC(vm, double)
|
|
and stores the box at *frame[1] (raw:17-34). atan2 / pow parse two doubles
|
|
(format "ff"): atan2(arg0, arg1), pow(base=arg0, exp=arg1) (raw:109-127,
|
|
386-406).
|
|
- the constant getters ignore their argument and box the exact retail double
|
|
(raw:225-333).
|
|
- random reads iggy_random() (a uint), scales it (float)v * 2**-32 through a
|
|
float32 round-trip, and boxes the resulting double (raw:410-424).
|
|
- Number_toString's `this` uses the as3vm_convert value tagging: (word & 1)==0
|
|
is a small int of value word>>1; (word & 3)==1 is a heap node at word-1 whose
|
|
KIND byte node[0]==7 marks a heap-number with its double at node+8 == word+7
|
|
(raw:566-575).
|
|
|
|
WHAT IS PINNED (real, deterministic DURANGO behavior):
|
|
- Math__construct raises exception 0x74 unconditionally (raw:9).
|
|
- abs -> fabs, ceil, floor, round -> floor(x + 0.5), and the acos / asin /
|
|
atan / atan2 / cos / exp / log / pow / sin / sqrt / tan direct libm
|
|
passthrough -- each boxes the exact libm double via make_number (raw:30-31,
|
|
147-149, 217-219, 443, and the trig/exp/log/pow bodies).
|
|
- round's AS3 half-up-toward-+Infinity rule: round(2.5)=3, round(-2.5)=-2
|
|
(a retail wart -- floor(x+0.5) -- pinned, not corrected) (raw:443).
|
|
- random's exact float32-rounded scaling: iggy_random()=0x80000000 -> 0.5,
|
|
iggy_random()=0 -> 0.0 (raw:420).
|
|
- each get_<CONST> boxes the exact retail bit pattern (raw:225-333):
|
|
E=0x4005bf0a8b145769 LN10=0x40026bb1bbb55516 LN2=0x3fe62e42fefa39ef
|
|
LOG10E=0x3fdbcb7b1526e50e LOG2E=0x3ff71547652b82fe PI=0x400921fb54442d18
|
|
SQRT1_2=0x3fe6a09e667f3bcd SQRT2=0x3ff6a09e667f3bcd.
|
|
- max / min fold their arguments through dmax / dmin seeded with -Inf / +Inf:
|
|
max()=-Inf, min()=+Inf, max(3,5,1)=5, min(3,5,1)=1 (raw:364-382, 733-761,
|
|
ps3:6-26), plus the NaN warts below.
|
|
- max / min NaN warts (the comparators are `if (arg-acc<0) acc=arg; return acc`,
|
|
which do NOT match ECMAScript): max(3, NaN)=NaN (NaN last captures) but
|
|
max(NaN, 3)=3 (NaN first is overwritten); min ignores NaN arguments entirely,
|
|
min(5, NaN)=5. Pinned as retail warts, NOT corrected.
|
|
- Number_toString radix 10 over a small int / heap-number `this` routes through
|
|
as3vm_string_from_number_GC with the exact double (raw:565-575), and a
|
|
radix-2..36 NaN / +Inf / -Inf returns the interned VM-context strings at
|
|
vm+0x27d1 / vm+0x27f1 / vm+0x2801 (raw:604-718).
|
|
- argument-parse failure (nonzero return) raises exception 0x78 (raw:26-28).
|
|
|
|
DEFERRED (flagged for the impl / review phase, NOT exercised here):
|
|
- Number_toString's arbitrary-radix (2..36) finite-number digit-conversion loop
|
|
(raw:585-704) -- a deep floating-point long division driving
|
|
as3vm_make_string_wc_GC; only its radix-10 numeric paths and its
|
|
NaN / +/-Infinity interned-string returns are pinned.
|
|
- the radix-10 zero-sentinel branch (raw:576-580) -- needs the live vm+0x2840
|
|
context table layout, pinned STRUCTURALLY ONLY (make_string_c_GC linked,
|
|
never asserted).
|
|
|
|
Full-suite runs use python3.12 (the machine python3 is 3.14, which SEGFAULTs on
|
|
the full suite via an unrelated CPython ctypes-GC bug); single-module runs of
|
|
this file are fine on either interpreter.
|
|
"""
|
|
|
|
import ctypes
|
|
import math
|
|
import shutil
|
|
from pathlib import Path
|
|
import struct
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
|
SOURCE = RECONSTRUCTED / "as3vm_imp_math.c"
|
|
HEADER = RECONSTRUCTED / "as3vm_imp_math.h"
|
|
SUPPORT = ROOT / "tests/support/as3vm_imp_math_test_support.c"
|
|
|
|
MASK64 = (1 << 64) - 1
|
|
|
|
# Exact retail Math constant bit patterns (durango __real@... rodata; raw:225-333).
|
|
MATH_CONSTANTS = {
|
|
"E": 0x4005BF0A8B145769,
|
|
"LN10": 0x40026BB1BBB55516,
|
|
"LN2": 0x3FE62E42FEFA39EF,
|
|
"LOG10E": 0x3FDBCB7B1526E50E,
|
|
"LOG2E": 0x3FF71547652B82FE,
|
|
"PI": 0x400921FB54442D18,
|
|
"SQRT1_2": 0x3FE6A09E667F3BCD,
|
|
"SQRT2": 0x3FF6A09E667F3BCD,
|
|
}
|
|
|
|
# DURANGO VM-context interned-string offsets returned by Number_toString for the
|
|
# NaN / +Inf / -Inf radix-2..36 cases (raw:709-717).
|
|
STR_NAN = 0x27D1
|
|
STR_PINF = 0x27F1
|
|
STR_NINF = 0x2801
|
|
|
|
# Number_toString `this` KIND byte for a heap-number node (raw:572 == '\a').
|
|
KIND_NUMBER = 7
|
|
|
|
|
|
def _bits_to_double(bits):
|
|
return struct.unpack("<d", struct.pack("<Q", bits & MASK64))[0]
|
|
|
|
|
|
def _random_expected(value):
|
|
# (double)((float)((uint)value) * (float)2**-32): two float32 roundings
|
|
# (raw:420 vcvtps2pd(ZEXT416((float)v * __real_2f800000))).
|
|
scale = struct.unpack("<f", struct.pack("<I", 0x2F800000))[0] # 2**-32 float
|
|
v32 = struct.unpack("<f", struct.pack("<f", float(value & 0xFFFFFFFF)))[0]
|
|
product = struct.unpack("<f", struct.pack("<f", v32 * scale))[0]
|
|
return float(product)
|
|
|
|
|
|
class ReconstructedAs3vmImpMathTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
missing = [str(path) for path in (SOURCE, HEADER, SUPPORT)
|
|
if not path.exists()]
|
|
if missing:
|
|
raise AssertionError(
|
|
"missing as3vm_imp_math runtime inputs: " + ", ".join(missing))
|
|
cls.temp = tempfile.TemporaryDirectory()
|
|
cls.base = Path(cls.temp.name) / "as3vm_imp_math.dylib"
|
|
build = subprocess.run([
|
|
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
|
|
"-Werror", "-Wl,-undefined,dynamic_lookup",
|
|
"-DIGGY_RECONSTRUCT_AS3VM_IMP_MATH_DURANGO=1",
|
|
"-I", str(RECONSTRUCTED),
|
|
str(SOURCE), str(SUPPORT), "-o", str(cls.base),
|
|
], capture_output=True, text=True)
|
|
if build.returncode:
|
|
raise AssertionError(build.stdout + build.stderr)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.temp.cleanup()
|
|
|
|
def setUp(self):
|
|
unique = (Path(self.temp.name) /
|
|
f"as3vm_imp_math_{self._testMethodName}.dylib")
|
|
shutil.copyfile(self.base, unique)
|
|
self.lib = ctypes.CDLL(str(unique))
|
|
self._bind(self.lib)
|
|
self.lib.as3vm_imp_math_test_reset()
|
|
self._keep = []
|
|
|
|
def _bind(self, lib):
|
|
void_p = ctypes.c_void_p
|
|
c_double = ctypes.c_double
|
|
|
|
lib.as3vm_imp_math_test_reset.restype = None
|
|
lib.as3vm_imp_math_test_reset.argtypes = []
|
|
for name in ("exception", "make_number", "string_from_number",
|
|
"make_string_c", "make_string_wc", "parse_args",
|
|
"tonumber", "random"):
|
|
fn = getattr(lib, f"as3vm_imp_math_test_{name}_count")
|
|
fn.restype = ctypes.c_size_t
|
|
fn.argtypes = []
|
|
lib.as3vm_imp_math_test_last_exception_code.restype = ctypes.c_int32
|
|
lib.as3vm_imp_math_test_last_exception_code.argtypes = []
|
|
for name in ("make_number", "string_from_number"):
|
|
fn = getattr(lib, f"as3vm_imp_math_test_last_{name}")
|
|
fn.restype = c_double
|
|
fn.argtypes = []
|
|
lib.as3vm_imp_math_test_set_parse_double.restype = None
|
|
lib.as3vm_imp_math_test_set_parse_double.argtypes = [
|
|
ctypes.c_int, c_double]
|
|
lib.as3vm_imp_math_test_set_parse_int.restype = None
|
|
lib.as3vm_imp_math_test_set_parse_int.argtypes = [ctypes.c_uint32]
|
|
lib.as3vm_imp_math_test_set_parse_status.restype = None
|
|
lib.as3vm_imp_math_test_set_parse_status.argtypes = [ctypes.c_int]
|
|
lib.as3vm_imp_math_test_set_tonumber_status.restype = None
|
|
lib.as3vm_imp_math_test_set_tonumber_status.argtypes = [ctypes.c_int]
|
|
lib.as3vm_imp_math_test_set_random_value.restype = None
|
|
lib.as3vm_imp_math_test_set_random_value.argtypes = [ctypes.c_uint32]
|
|
|
|
# 28 module exports (durango 64-bit ABI); every thunk takes the frame.
|
|
for name in (
|
|
"as3vm_Math__construct", "as3vm_Math_abs", "as3vm_Math_acos",
|
|
"as3vm_Math_asin", "as3vm_Math_atan", "as3vm_Math_atan2",
|
|
"as3vm_Math_ceil", "as3vm_Math_cos", "as3vm_Math_exp",
|
|
"as3vm_Math_floor", "as3vm_Math_get_E", "as3vm_Math_get_LN10",
|
|
"as3vm_Math_get_LN2", "as3vm_Math_get_LOG10E",
|
|
"as3vm_Math_get_LOG2E", "as3vm_Math_get_PI",
|
|
"as3vm_Math_get_SQRT1_2", "as3vm_Math_get_SQRT2", "as3vm_Math_log",
|
|
"as3vm_Math_max", "as3vm_Math_min", "as3vm_Math_pow",
|
|
"as3vm_Math_random", "as3vm_Math_round", "as3vm_Math_sin",
|
|
"as3vm_Math_sqrt", "as3vm_Math_tan", "as3vm_Number_toString",
|
|
):
|
|
fn = getattr(lib, name)
|
|
fn.restype = None
|
|
fn.argtypes = [void_p]
|
|
|
|
def _count(self, name):
|
|
return getattr(self.lib, f"as3vm_imp_math_test_{name}_count")()
|
|
|
|
def _buffer(self, size):
|
|
buf = (ctypes.c_uint8 * size)()
|
|
self._keep.append(buf)
|
|
return ctypes.addressof(buf)
|
|
|
|
# ---- Builders. ---- #
|
|
|
|
def _vm(self):
|
|
# A VM buffer large enough for the Number_toString interned-string
|
|
# offsets (vm + 0x27xx / 0x28xx) to land inside.
|
|
return self._buffer(0x3000)
|
|
|
|
def _frame(self, vm, this_value, argc, args=()):
|
|
# frame[0]=vm, frame[1]=&result, frame[2]=argc, frame[3]=&this-slot;
|
|
# the this-slot holds [this, arg0, arg1, ...] (raw:6-34, 733-761).
|
|
result = self._buffer(8)
|
|
slot = self._buffer(8 * (1 + len(args)))
|
|
ctypes.c_uint64.from_address(slot).value = this_value & MASK64
|
|
for i, value in enumerate(args):
|
|
ctypes.c_uint64.from_address(slot + (i + 1) * 8).value = (
|
|
value & MASK64)
|
|
frame = self._buffer(0x20)
|
|
ctypes.c_uint64.from_address(frame + 0x00).value = vm
|
|
ctypes.c_uint64.from_address(frame + 0x08).value = result
|
|
ctypes.c_int32.from_address(frame + 0x10).value = argc
|
|
ctypes.c_uint64.from_address(frame + 0x18).value = slot
|
|
return frame, result
|
|
|
|
def _double_arg(self, value):
|
|
# An argument word that is a pointer to a caller-owned double; the
|
|
# ToNumberDirect harness stub reads it straight through (max/min fold).
|
|
buf = self._buffer(8)
|
|
ctypes.c_double.from_address(buf).value = value
|
|
return buf
|
|
|
|
def _heap_number(self, value):
|
|
# Number_toString heap-number `this`: node[0]=7, double at node+8,
|
|
# tagged word = node+1 (raw:572-573).
|
|
node = self._buffer(0x20)
|
|
ctypes.c_uint8.from_address(node).value = KIND_NUMBER
|
|
ctypes.c_double.from_address(node + 8).value = value
|
|
return node + 1
|
|
|
|
def _set_parse(self, *doubles):
|
|
for i, value in enumerate(doubles):
|
|
self.lib.as3vm_imp_math_test_set_parse_double(i, value)
|
|
|
|
def _result(self, result):
|
|
return ctypes.c_uint64.from_address(result).value
|
|
|
|
def _last_number(self):
|
|
return self.lib.as3vm_imp_math_test_last_make_number()
|
|
|
|
def _call_unary(self, fn, value):
|
|
# Drive a unary Math method whose double comes from the parse stub.
|
|
vm = self._vm()
|
|
self._set_parse(value)
|
|
frame, _ = self._frame(vm, this_value=0, argc=1, args=(0,))
|
|
fn(frame)
|
|
return self._last_number()
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# Math__construct -- always throws.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_construct_raises_0x74(self):
|
|
# raw:9 the Math class constructor unconditionally raises 0x74.
|
|
vm = self._vm()
|
|
frame, _ = self._frame(vm, this_value=0, argc=0)
|
|
self.lib.as3vm_Math__construct(frame)
|
|
self.assertEqual(self._count("exception"), 1)
|
|
self.assertEqual(
|
|
self.lib.as3vm_imp_math_test_last_exception_code(), 0x74)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# Direct libm passthroughs -- exact boxed double.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_abs_is_fabs(self):
|
|
# raw:30 abs boxes fabs(x).
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_abs, -3.5), 3.5)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_abs, 3.5), 3.5)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_abs, 0.0), 0.0)
|
|
|
|
def test_ceil_floor(self):
|
|
# raw:147/217 ceil / floor passthrough.
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_ceil, 2.1), 3.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_ceil, -2.1), -2.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_floor, 2.9), 2.0)
|
|
self.assertEqual(
|
|
self._call_unary(self.lib.as3vm_Math_floor, -2.1), -3.0)
|
|
|
|
def test_round_half_up_toward_positive_infinity(self):
|
|
# raw:443 round == floor(x + 0.5): the AS3 half-up-toward-+Inf wart.
|
|
cases = {2.5: 3.0, 2.4: 2.0, 2.6: 3.0, -2.5: -2.0, -2.6: -3.0,
|
|
0.5: 1.0, -0.5: 0.0, 0.0: 0.0}
|
|
for value, expected in cases.items():
|
|
self.assertEqual(
|
|
self._call_unary(self.lib.as3vm_Math_round, value), expected,
|
|
value)
|
|
|
|
def test_trig_exp_log_exact(self):
|
|
# raw bodies: sin/cos/tan/asin/acos/atan/exp/log/sqrt direct libm.
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_sin, 0.0), 0.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_cos, 0.0), 1.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_tan, 0.0), 0.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_asin, 0.0), 0.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_acos, 1.0), 0.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_atan, 0.0), 0.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_exp, 0.0), 1.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_log, 1.0), 0.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_sqrt, 4.0), 2.0)
|
|
self.assertEqual(self._call_unary(self.lib.as3vm_Math_sqrt, 2.0),
|
|
math.sqrt(2.0))
|
|
|
|
def test_atan2_and_pow_two_args(self):
|
|
# raw:123 atan2(arg0, arg1); raw:402 pow(base=arg0, exp=arg1).
|
|
vm = self._vm()
|
|
self._set_parse(0.0, 1.0)
|
|
frame, _ = self._frame(vm, this_value=0, argc=2, args=(0, 0))
|
|
self.lib.as3vm_Math_atan2(frame)
|
|
self.assertEqual(self._last_number(), 0.0)
|
|
|
|
self._set_parse(1.0, 0.0)
|
|
frame, _ = self._frame(vm, this_value=0, argc=2, args=(0, 0))
|
|
self.lib.as3vm_Math_atan2(frame)
|
|
self.assertEqual(self._last_number(), math.atan2(1.0, 0.0))
|
|
|
|
self._set_parse(2.0, 10.0)
|
|
frame, _ = self._frame(vm, this_value=0, argc=2, args=(0, 0))
|
|
self.lib.as3vm_Math_pow(frame)
|
|
self.assertEqual(self._last_number(), 1024.0)
|
|
|
|
def test_parse_failure_raises_0x78(self):
|
|
# raw:26-28 a nonzero parse return raises exception 0x78.
|
|
vm = self._vm()
|
|
self.lib.as3vm_imp_math_test_set_parse_status(1)
|
|
frame, _ = self._frame(vm, this_value=0, argc=0)
|
|
self.lib.as3vm_Math_sin(frame)
|
|
self.assertEqual(self._count("exception"), 1)
|
|
self.assertEqual(
|
|
self.lib.as3vm_imp_math_test_last_exception_code(), 0x78)
|
|
self.assertEqual(self._count("make_number"), 0)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# random -- exact float32-rounded scaling.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_random_scaling(self):
|
|
# raw:420 (double)((float)iggy_random() * 2**-32) with float32 rounding.
|
|
for value in (0x00000000, 0x80000000, 0x00000001, 0x40000000):
|
|
self.lib.as3vm_imp_math_test_reset()
|
|
self.lib.as3vm_imp_math_test_set_random_value(value)
|
|
vm = self._vm()
|
|
frame, _ = self._frame(vm, this_value=0, argc=0)
|
|
self.lib.as3vm_Math_random(frame)
|
|
self.assertEqual(self._count("random"), 1)
|
|
self.assertEqual(self._last_number(), _random_expected(value),
|
|
hex(value))
|
|
# The canonical half point is exact.
|
|
self.lib.as3vm_imp_math_test_reset()
|
|
self.lib.as3vm_imp_math_test_set_random_value(0x80000000)
|
|
vm = self._vm()
|
|
frame, _ = self._frame(vm, this_value=0, argc=0)
|
|
self.lib.as3vm_Math_random(frame)
|
|
self.assertEqual(self._last_number(), 0.5)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# Constant getters -- exact retail bit patterns.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_constant_getters_exact_doubles(self):
|
|
# raw:225-333 each getter boxes the exact retail double.
|
|
getters = {
|
|
"E": self.lib.as3vm_Math_get_E,
|
|
"LN10": self.lib.as3vm_Math_get_LN10,
|
|
"LN2": self.lib.as3vm_Math_get_LN2,
|
|
"LOG10E": self.lib.as3vm_Math_get_LOG10E,
|
|
"LOG2E": self.lib.as3vm_Math_get_LOG2E,
|
|
"PI": self.lib.as3vm_Math_get_PI,
|
|
"SQRT1_2": self.lib.as3vm_Math_get_SQRT1_2,
|
|
"SQRT2": self.lib.as3vm_Math_get_SQRT2,
|
|
}
|
|
for name, fn in getters.items():
|
|
self.lib.as3vm_imp_math_test_reset()
|
|
vm = self._vm()
|
|
frame, _ = self._frame(vm, this_value=0, argc=0)
|
|
fn(frame)
|
|
self.assertEqual(self._count("make_number"), 1, name)
|
|
boxed = struct.pack("<d", self._last_number())
|
|
self.assertEqual(struct.unpack("<Q", boxed)[0],
|
|
MATH_CONSTANTS[name], name)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# max / min -- fold + seed + NaN warts.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def _call_fold(self, fn, values):
|
|
vm = self._vm()
|
|
args = tuple(self._double_arg(v) for v in values)
|
|
frame, _ = self._frame(vm, this_value=0, argc=len(values), args=args)
|
|
fn(frame)
|
|
return self._last_number()
|
|
|
|
def test_max_min_no_args_return_seed_infinities(self):
|
|
# raw:367/380 max() seeds -Inf, min() seeds +Inf; with no args the seed
|
|
# is boxed directly.
|
|
self.assertEqual(self._call_fold(self.lib.as3vm_Math_max, ()),
|
|
float("-inf"))
|
|
self.assertEqual(self._call_fold(self.lib.as3vm_Math_min, ()),
|
|
float("inf"))
|
|
|
|
def test_max_min_fold_plain_numbers(self):
|
|
# raw:733-761 + ps3:6-26 fold via dmax / dmin.
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_max, (3.0, 5.0, 1.0)), 5.0)
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_min, (3.0, 5.0, 1.0)), 1.0)
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_max, (-4.0, -9.0)), -4.0)
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_min, (-4.0, -9.0)), -9.0)
|
|
|
|
def test_max_nan_wart_is_position_dependent(self):
|
|
# ps3:6-12 dmax(acc, arg) = `if (arg-acc<0) acc=arg; return acc`.
|
|
# A NaN as the LAST argument is captured; a NaN followed by a finite
|
|
# value is OVERWRITTEN -- NOT the ECMAScript "any NaN -> NaN" rule.
|
|
self.assertTrue(
|
|
math.isnan(self._call_fold(self.lib.as3vm_Math_max,
|
|
(3.0, float("nan")))))
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_max, (float("nan"), 3.0)), 3.0)
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_max,
|
|
(3.0, float("nan"), 1.0)), 1.0)
|
|
|
|
def test_min_ignores_nan_wart(self):
|
|
# ps3:19-25 dmin(acc, arg) returns acc whenever arg is NaN, so min
|
|
# silently drops NaN arguments (again NOT the ECMAScript rule).
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_min, (5.0, float("nan"))), 5.0)
|
|
self.assertEqual(
|
|
self._call_fold(self.lib.as3vm_Math_min,
|
|
(5.0, float("nan"), 3.0)), 3.0)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# Number_toString -- radix 10 numeric + NaN/Inf interned strings.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_number_tostring_radix10_small_int(self):
|
|
# raw:566-571 radix 10 + small-int `this` -> string_from_number((double)v).
|
|
vm = self._vm()
|
|
frame, result = self._frame(
|
|
vm, this_value=(5 << 1), argc=0) # small int 5
|
|
self.lib.as3vm_Number_toString(frame)
|
|
self.assertEqual(self._count("string_from_number"), 1)
|
|
self.assertEqual(
|
|
self.lib.as3vm_imp_math_test_last_string_from_number(), 5.0)
|
|
self.assertNotEqual(self._result(result), 0)
|
|
|
|
def test_number_tostring_radix10_heap_number(self):
|
|
# raw:572-575 radix 10 + heap-number `this` -> string_from_number(double).
|
|
vm = self._vm()
|
|
frame, _ = self._frame(vm, this_value=self._heap_number(3.5), argc=0)
|
|
self.lib.as3vm_Number_toString(frame)
|
|
self.assertEqual(self._count("string_from_number"), 1)
|
|
self.assertEqual(
|
|
self.lib.as3vm_imp_math_test_last_string_from_number(), 3.5)
|
|
|
|
def test_number_tostring_radix2_nan_and_infinities(self):
|
|
# raw:604-718 a radix in 2..36 over a NaN / +Inf / -Inf heap-number
|
|
# returns the interned VM-context strings at fixed offsets.
|
|
for value, offset in ((float("nan"), STR_NAN),
|
|
(float("inf"), STR_PINF),
|
|
(float("-inf"), STR_NINF)):
|
|
self.lib.as3vm_imp_math_test_reset()
|
|
self.lib.as3vm_imp_math_test_set_parse_int(2) # radix 2
|
|
vm = self._vm()
|
|
frame, result = self._frame(
|
|
vm, this_value=self._heap_number(value), argc=1, args=(0,))
|
|
self.lib.as3vm_Number_toString(frame)
|
|
self.assertEqual(self._result(result), vm + offset, value)
|
|
self.assertEqual(self._count("string_from_number"), 0, value)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|