#! /usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/QUnit.qm

%exec-class ToBaseTest

public class ToBaseTest inherits QUnit::Test {
    constructor() : Test("ToBaseTest", "1.1") {
        addTestCase("Test <int>::toBase()", \testIntToBase(), NOTHING);
        addTestCase("Test <float>::toBase()", \testFloatToBase(), NOTHING);
        addTestCase("Test <number>::toBase()", \testNumberToBase(), NOTHING);
        set_return_value(main());
    }

    testIntToBase() {
        assertEq("11011", 27.toBase(2));
        assertEq("11011", 033.toBase(2));
        assertEq("11011", 0x1B.toBase(2));

        assertEq("-102", (-27).toBase(5));
        assertEq("-102", (-0x1B).toBase(5));

        assertEq("33", 27.toBase(8));
        assertEq("33", 033.toBase(8));
        assertEq("33", 0x1B.toBase(8));

        assertEq("27", 033.toBase());
        assertEq("27", 0x1B.toBase());

        assertEq("-21", (-27).toBase(13));

        assertEq("1B", 27.toBase(16));
        assertEq("1B", 0x1B.toBase(16));

        assertEq("10", 27.toBase(27));
        assertEq("-R", (-27).toBase(28));
        assertEq("R", 27.toBase(36));
        assertEq("Z", 35.toBase(36));
    }

    testNumberToBase() {
        assertEq("1111010110011101001100000011011011010110000000110000100111", 276536798792387623n.toBase(2));
        assertEq("-MJ934KRG1O3R", (-276536798792387623n).toBase(29));
        assertEq("F.A", (15.5n).toBase(20));
        assertEq("-D.G", (-13.8n).toBase(20));
        assertEq("16.73", (21.48n).toBase(15));
    }

    testFloatToBase() {
        assertEq("F.A", (15.5).toBase(20));
        assertFloatEq(float("-D.G"), float((-13.8).toBase(20)));
        assertEq("16.73", (21.48).toBase(15));
    }
}
