static int failures = 0;

void expect_string(string label, PhpefRuntimeCore.Zval actual, string expected) {
    string actual_text = actual.to_php_string();
    if (actual_text != expected) {
        stderr.printf("%s: expected '%s', got '%s'\n", label, expected, actual_text);
        failures++;
    }
}

void expect_float(string label, PhpefRuntimeCore.Zval actual, double expected) {
    double actual_value = actual.to_float_zval().as_float();
    double delta = actual_value - expected;
    if (delta < 0.0) {
        delta = -delta;
    }
    if (delta > 0.000001) {
        stderr.printf("%s: expected %.8f, got %.8f\n", label, expected, actual_value);
        failures++;
    }
}

int main(string[] args) {
    PhpefRuntimeCore.init(args);

    expect_float("floatval numeric string", PhpefRuntimeNativeStandard.phpfunc_floatval(PhpefRuntimeCore.Zval.from_string("12.5")), 12.5);
    expect_float("floor positive", PhpefRuntimeNativeStandard.phpfunc_floor(PhpefRuntimeCore.Zval.from_float(12.9)), 12.0);
    expect_float("floor negative", PhpefRuntimeNativeStandard.phpfunc_floor(PhpefRuntimeCore.Zval.from_float(-12.1)), -13.0);
    expect_float("round default", PhpefRuntimeNativeStandard.phpfunc_round(PhpefRuntimeCore.Zval.from_float(12.5)), 13.0);
    expect_float("round precision", PhpefRuntimeNativeStandard.phpfunc_round(PhpefRuntimeCore.Zval.from_float(12.345), PhpefRuntimeCore.Zval.from_int(2)), 12.35);
    expect_string("number_format default", PhpefRuntimeNativeStandard.phpfunc_number_format(PhpefRuntimeCore.Zval.from_int(1234567)), "1,234,567");
    expect_string("number_format decimals", PhpefRuntimeNativeStandard.phpfunc_number_format(PhpefRuntimeCore.Zval.from_float(1234.567), PhpefRuntimeCore.Zval.from_int(2)), "1,234.57");
    expect_string("number_format separators", PhpefRuntimeNativeStandard.phpfunc_number_format(PhpefRuntimeCore.Zval.from_float(1234.5), PhpefRuntimeCore.Zval.from_int(1), PhpefRuntimeCore.Zval.from_string(","), PhpefRuntimeCore.Zval.from_string(".")), "1.234,5");

    if (failures != 0) {
        return 1;
    }
    stdout.printf("ok - native standard math wrappers\n");
    return 0;
}
