name:
src/test/js/execution/CodeExecutorGeneralExponentTest.ts
-rw-r--r--
2335
1import { assert } from "chai";
2import { it, describe } from "../testutils/TestUtils";
3import { F7Exception } from "../../../main/js/errors/F7Exception";
4import { F7ExceptionName } from "../../../main/js/errors/F7ExceptionName";
5import { run } from "../testutils/TestUtils";
6
7describe("CodeExecutor - General Exponent Test", function () {
8 it("should work with positive number and positive exponent", function () {
9 assert.deepEqual(run("3^5"), 243);
10 assert.deepEqual(run("4^5"), 1024);
11 assert.deepEqual(run("1^1"), 1);
12 assert.deepEqual(run("1^5"), 1);
13 assert.deepEqual(run("2^0"), 1);
14 assert.deepEqual(run("0^10"), 0);
15 });
16
17 it("should with positive number and negative exponent", function () {
18 assert.deepEqual(run("3^-5"), 0.00411522633744856);
19 assert.deepEqual(run("4^-5"), 9.765625e-4);
20 assert.deepEqual(run("1^-1"), 1);
21 assert.deepEqual(run("1^-5"), 1);
22 assert.deepEqual(run("2^-0"), 1);
23 // TODO/HACK: This is different in Excel and Google Sheets.
24 assert.deepEqual((run("0^-10") as F7Exception).name, F7ExceptionName.NUM);
25 });
26
27 it("should work with negative number and negative exponent", function () {
28 assert.deepEqual(run("-3^-1"), -0.3333333333333333);
29 assert.deepEqual(run("-3^-2"), 0.1111111111111111);
30 assert.deepEqual(run("-3^-3"), -0.037037037037037035);
31 assert.deepEqual(run("-2^-3"), -0.125);
32 assert.deepEqual(run("-1^-3"), -1);
33 assert.deepEqual(run("-1^-2"), 1);
34 });
35
36 it("should work with negative number and positive exponent", function () {
37 assert.deepEqual(run("-3^1"), -3);
38 assert.deepEqual(run("-3^2"), 9);
39 assert.deepEqual(run("-3^3"), -27);
40 assert.deepEqual(run("-2^3"), -8);
41 assert.deepEqual(run("-1^3"), -1);
42 assert.deepEqual(run("-1^2"), 1);
43 });
44
45 it("should perform with proper associative property", function () {
46 assert.deepEqual(run("2 ^ 3 ^ 4"), 4096);
47 assert.deepEqual(run("3 ^ 2 ^ 4"), 6561);
48 assert.deepEqual(run("4 ^ 2 ^ 3"), 4096);
49 });
50
51 it("should work with booleans", function () {
52 assert.deepEqual(run("9 ^ TRUE"), 9);
53 assert.deepEqual(run("9 ^ FALSE"), 1);
54 });
55
56 it("should work with strings", function () {
57 assert.deepEqual(run('2 ^ "3"'), 8);
58 assert.deepEqual(run('2 ^ "3e1"'), 1.073741824e9);
59 assert.deepEqual(run('2 ^ "2"'), 4);
60 });
61});