github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/instruction_counter_tests/switch.js (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 const assert = require('assert.js'); 19 20 function assertEqual(func, args, expected, expected_count, msg) { 21 const count_of_helper_statement = 46; 22 var count = _instruction_counter.count; 23 assert.equal(func.apply(null, args), expected); 24 assert.equal(_instruction_counter.count - count - count_of_helper_statement, expected_count, msg); 25 }; 26 27 28 // test1. 29 var gen1X = function (a) { 30 var o = {}; 31 o.get = function () { 32 return a; 33 } 34 return o; 35 } 36 var test1 = function (x) { 37 var ret = 0; 38 switch (x.get()) { 39 case 1: 40 ret = 1 + 2; 41 break; 42 case 2: 43 ret = 2 * 2; 44 break; 45 case 3: 46 ret = 3 * 3; 47 case 4: 48 ret += 4 * 4; 49 break; 50 case 100: 51 return 100; 52 case 101: 53 return 3 * x.get(); 54 default: 55 ret = x.get() * 2 + 13; 56 } 57 return ret; 58 }; 59 assertEqual(test1, [gen1X(1)], 3, 18); 60 assertEqual(test1, [gen1X(2)], 4, 18); 61 assertEqual(test1, [gen1X(3)], 25, 24); 62 assertEqual(test1, [gen1X(4)], 16, 18); 63 assertEqual(test1, [gen1X(100)], 100, 12); 64 assertEqual(test1, [gen1X(101)], 303, 27); 65 assertEqual(test1, [gen1X(50)], 113, 33); 66 67 // test2. 68 var gen2X = function (a) { 69 return [a]; 70 } 71 var test2 = function (x) { 72 var ret = 0; 73 switch (x[0]) { 74 case 1: 75 ret = 1 + 2; 76 break; 77 case 2: 78 ret = 2 * 2; 79 break; 80 case 3: 81 ret = 3 * 3; 82 case 4: 83 ret += 4 * 4; 84 break; 85 case 100: 86 return 100; 87 case 101: 88 return 3 * x[0]; 89 default: 90 ret = x[0] * 2 + 13; 91 } 92 return ret; 93 }; 94 assertEqual(test2, [gen2X(1)], 3, 10); 95 assertEqual(test2, [gen2X(2)], 4, 10); 96 assertEqual(test2, [gen2X(3)], 25, 16); 97 assertEqual(test2, [gen2X(4)], 16, 10); 98 assertEqual(test2, [gen2X(100)], 100, 4); 99 assertEqual(test2, [gen2X(101)], 303, 11); 100 assertEqual(test2, [gen2X(50)], 113, 17);