1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { expect } from 'chai'; 17import * as ts from "typescript"; 18import { compileNumericLiteral } from "../../src/expression/numericLiteral"; 19import { 20 Fldai, 21 Imm, 22 Lda, 23 Ldai, 24 VReg, 25} from "../../src/irnodes"; 26import { PandaGen } from "../../src/pandagen"; 27import { checkInstructions } from "../utils/base"; 28import { creatAstFromSnippet } from "../utils/asthelper" 29 30describe("compileNumericLiteral", function () { 31 it("NaN", function () { 32 let pandaGen = new PandaGen("ignored", creatAstFromSnippet("NaN"), 0, undefined); 33 let node: ts.NumericLiteral = ts.createNumericLiteral("NaN"); 34 compileNumericLiteral(pandaGen, node); 35 let insns = pandaGen.getInsns(); 36 let expected = [new Lda(new VReg())]; 37 expect(checkInstructions(insns, expected)).to.be.true; 38 }); 39 40 it("Infinity", function () { 41 let pandaGen = new PandaGen("ignored", creatAstFromSnippet("10e10000"), 0, undefined); 42 let node: ts.NumericLiteral = ts.createNumericLiteral("10e10000"); 43 compileNumericLiteral(pandaGen, node); 44 let insns = pandaGen.getInsns(); 45 let expected = [new Lda(new VReg())]; 46 expect(checkInstructions(insns, expected)).to.be.true; 47 }); 48 it("int", function () { 49 let pandaGen = new PandaGen("ignored", creatAstFromSnippet("1"), 0, undefined); 50 let node: ts.NumericLiteral = ts.createNumericLiteral("1"); 51 compileNumericLiteral(pandaGen, node); 52 let insns = pandaGen.getInsns(); 53 let expected = [new Ldai(new Imm(1))]; 54 expect(checkInstructions(insns, expected)).to.be.true; 55 }); 56 it("Integer overflow", function () { 57 let pandaGen = new PandaGen("ignored", creatAstFromSnippet("2147483648"), 0, undefined); 58 let node: ts.NumericLiteral = ts.createNumericLiteral("2147483648"); 59 compileNumericLiteral(pandaGen, node); 60 let insns = pandaGen.getInsns(); 61 let expected = [new Fldai(new Imm(2147483648))]; 62 expect(checkInstructions(insns, expected)).to.be.true; 63 }); 64 it("double", function () { 65 let pandaGen = new PandaGen("ignored", creatAstFromSnippet("1.1"), 0, undefined); 66 let node: ts.NumericLiteral = ts.createNumericLiteral("1.1"); 67 compileNumericLiteral(pandaGen, node); 68 let insns = pandaGen.getInsns(); 69 let expected = [new Fldai(new Imm(1.1))]; 70 expect(checkInstructions(insns, expected)).to.be.true; 71 }); 72})