Procházet zdrojové kódy

Add --math-overflow test

Signed-off-by: Sean Young <sean@mess.org>
Sean Young před 2 roky
rodič
revize
77614559be

+ 17 - 0
integration/solana/overflow.sol

@@ -0,0 +1,17 @@
+contract overflow {
+	function addu32(uint32 a, uint32 b) public pure returns (uint32 c) {
+		c = a + b;
+	}
+
+	function subu32(uint32 a, uint32 b) public pure returns (uint32 c) {
+		c = a - b;
+	}
+
+	function mulu32(uint32 a, uint32 b) public pure returns (uint32 c) {
+		c = a * b;
+	}
+
+	function powu32(uint32 a, uint32 b) public pure returns (uint32 c) {
+		c = a ** b;
+	}
+}

+ 33 - 0
integration/solana/overflow.spec.ts

@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: Apache-2.0
+
+import expect from 'expect';
+import { loadContract } from './setup';
+import { BN } from '@project-serum/anchor';
+
+describe('Testing math overflow', function () {
+    this.timeout(500000);
+
+    it('overflow', async function () {
+        let { program } = await loadContract('overflow');
+
+        let res = await program.methods.addu32(new BN(1), new BN(3)).view();
+        expect(res).toEqual(4);
+
+        await expect(program.methods.addu32(new BN(2147483648), new BN(2147483648)).view()).rejects.toThrow();
+
+        res = await program.methods.subu32(new BN(7), new BN(3)).view();
+        expect(res).toEqual(4);
+
+        await expect(program.methods.subu32(new BN(2147483640), new BN(2147483648)).view()).rejects.toThrow();
+
+        res = await program.methods.mulu32(new BN(7), new BN(3)).view();
+        expect(res).toEqual(21);
+
+        await expect(program.methods.mulu32(new BN(2147483640), new BN(2147483648)).view()).rejects.toThrow();
+
+        res = await program.methods.powu32(new BN(7), new BN(3)).view();
+        expect(res).toEqual(343);
+
+        await expect(program.methods.powu32(new BN(2147483640), new BN(2147483648)).view()).rejects.toThrow();
+    });
+});

+ 1 - 1
integration/solana/package.json

@@ -4,7 +4,7 @@
   "description": "Integration tests with Solang and Solana",
   "scripts": {
     "test": "tsc; ts-node setup.ts; mocha --parallel *.spec.ts",
-    "build": "solang compile  *.sol --target solana -v"
+    "build": "solang compile *.sol --target solana -v; solang compile --math-overflow overflow.sol --target solana -v"
   },
   "author": "Sean Young <sean@mess.org>",
   "license": "MIT",