Skip to content

Commit

Permalink
Testcases adjusts and new ones for warning not initialized.
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Feb 9, 2024
1 parent 881de59 commit ae29dfa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/__tests__/misc.a.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,44 +213,44 @@ describe('Wrong code to check error safeguards', () => {

describe('Warnings', () => {
it('should compile with warning: right side of operator', () => {
const code = 'long la, lb; fixed fa, fb; fa = fb - la;'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nSET @fa $fb\nSET @r0 $la\nMUL @r0 $f100000000\nSUB @fa $r0\nFIN\n'
const code = 'long la=0, lb; fixed fa, fb=0.0; fa = fb - la;'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nCLR @la\nCLR @fb\nSET @fa $fb\nSET @r0 $la\nMUL @r0 $f100000000\nSUB @fa $r0\nFIN\n'
const warnings = "Warning: at line 1. Implicit type casting conversion on right side of operator '-'."
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
expect(compiler.getAssemblyCode()).toBe(assembly)
expect(compiler.getMachineCode().Warnings).toBe(warnings)
})
it('should compile with warning: left side of operator', () => {
const code = 'long la, lb; fixed fa, fb; fa = la - fb;'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nSET @fa $la\nMUL @fa $f100000000\nSUB @fa $fb\nFIN\n'
const code = 'long la=2, lb; fixed fa, fb=0.0; fa = la - fb;\n#pragma optimizationLevel 0'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nSET @la #0000000000000002\nCLR @fb\nSET @fa $la\nMUL @fa $f100000000\nSUB @fa $fb\nFIN\n'
const warnings = "Warning: at line 1. Implicit type casting conversion on left side of operator '-'."
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
expect(compiler.getAssemblyCode()).toBe(assembly)
expect(compiler.getMachineCode().Warnings).toBe(warnings)
})
it('should compile with warning: rigth side of comparision', () => {
const code = 'long la, lb; fixed fa, fb; if(fa < lb) la++;'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nSET @r0 $lb\nMUL @r0 $f100000000\nBLT $fa $r0 :__opt_1\nFIN\n__opt_1:\nINC @la\nFIN\n'
const code = 'long la, lb=10; fixed fa=1.0; if(fa < lb) la=1;\n#pragma optimizationLevel 0'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n\nSET @lb #000000000000000a\nSET @fa $f100000000\nSET @r0 $lb\nMUL @r0 $f100000000\nBGE $fa $r0 :__if1_endif\n__if1_start:\nSET @la #0000000000000001\n__if1_endif:\nFIN\n'
const warnings = "Warning: at line 1. Implicit type casting conversion on right side of comparision '<'."
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
expect(compiler.getAssemblyCode()).toBe(assembly)
expect(compiler.getMachineCode().Warnings).toBe(warnings)
})
it('should compile with warning: left side of comparision', () => {
const code = 'long la, lb; fixed fa, fb; if(la < fb) la++;'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nSET @r0 $la\nMUL @r0 $f100000000\nBLT $r0 $fb :__opt_1\nFIN\n__opt_1:\nINC @la\nFIN\n'
const code = 'long la=1, lb; fixed fa, fb=1.1; if(la < fb) la++;\n#pragma optimizationLevel 0\n'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nSET @la #0000000000000001\nSET @fb #00000000068e7780\nSET @r0 $la\nMUL @r0 $f100000000\nBGE $r0 $fb :__if1_endif\n__if1_start:\nINC @la\n__if1_endif:\nFIN\n'
const warnings = "Warning: at line 1. Implicit type casting conversion on left side of comparision '<'."
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
expect(compiler.getAssemblyCode()).toBe(assembly)
expect(compiler.getMachineCode().Warnings).toBe(warnings)
})
it('should compile with warning: right side of assignment', () => {
const code = 'long la, lb; fixed fa, fb; la = fb;'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nSET @la $fb\nDIV @la $f100000000\nFIN\n'
const code = 'long la, lb; fixed fa, fb=0.0; la = fb;'
const assembly = '^declare r0\n^declare r1\n^declare r2\n^declare f100000000\n^const SET @f100000000 #0000000005f5e100\n^declare la\n^declare lb\n^declare fa\n^declare fb\n\nCLR @fb\nSET @la $fb\nDIV @la $f100000000\nFIN\n'
const warnings = "Warning: at line 1. Implicit type casting conversion on right side of assignment '='."
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
Expand Down
16 changes: 15 additions & 1 deletion src/__tests__/warnings.a.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@ import { SmartC } from '../smartc'

describe('Warnings', () => {
it('should not warn: add or sub pointers with longs values (struct_ptr)', () => {
const code = 'search(2);\nstruct PLAYER { long address, balance, VDLS; } *playerPtr, players[2];\nstruct PLAYER * search(long playerAddress) {\nlong nPlayers;\n struct PLAYER * foundPlayer;\n foundPlayer = &players[0];\n for (long auxI = 0; auxI < nPlayers; auxI++) {\n if (foundPlayer->address == playerAddress) {\n return foundPlayer;\n }\n foundPlayer += (sizeof(struct PLAYER));\n foundPlayer = foundPlayer + (sizeof(struct PLAYER));\n foundPlayer++;\n }\n return NULL;\n}'
const code = 'search(2);\nstruct PLAYER { long address, balance, VDLS; } *playerPtr, players[2];\nstruct PLAYER * search(long playerAddress) {\nlong nPlayers=0;\n struct PLAYER * foundPlayer;\n foundPlayer = &players[0];\n for (long auxI = 0; auxI < nPlayers; auxI++) {\n if (foundPlayer->address == playerAddress) {\n return foundPlayer;\n }\n foundPlayer += (sizeof(struct PLAYER));\n foundPlayer = foundPlayer + (sizeof(struct PLAYER));\n foundPlayer++;\n }\n return NULL;\n}'
const warnings = ''
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
expect(compiler.getMachineCode().Warnings).toBe(warnings)
})
it('should warn: longs used before initialization', () => {
const code = 'struct PLAYER { long sa, sb; } player;\nlong a, b;\nlong message[4];\n\na+=1;\na = message[b];\na = player.sb;\n'
const warnings = "Warning: at line 5. Variable 'a' is used but not initialized.\nWarning: at line 6. Variable 'b' is used but not initialized.\nWarning: at line 7. Variable 'player_sb' is used but not initialized."
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
expect(compiler.getMachineCode().Warnings).toBe(warnings)
})
it('should not warn: same as before but initialized', () => {
const code = 'struct PLAYER { long sa, sb; } player;\nlong a, b;\nlong message[4];\n\na=b=1;\na = message[b];\nplayer.sb=message[2];\na = player.sb;\n'
const warnings = ''
const compiler = new SmartC({ language: 'C', sourceCode: code })
compiler.compile()
Expand Down

0 comments on commit ae29dfa

Please sign in to comment.