github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/instruction_counter_tests/if.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  // test 1.
    28  var test1 = function (x) {
    29      if (x < 5) {
    30          return 0;
    31      } else if (x < 20) {
    32          return 5;
    33      } else if (x < 40) {
    34          return 10;
    35      } else {
    36          return -1;
    37      }
    38  };
    39  assertEqual(test1, [1], 0, 3);
    40  assertEqual(test1, [15], 5, 6);
    41  assertEqual(test1, [30], 10, 9);
    42  assertEqual(test1, [100], -1, 12);
    43  
    44  // test 2.
    45  var test2 = function (x, y) {
    46      if (x < 5 && x < y) {
    47          return 0;
    48      } else if (x < 50 && x < y) {
    49          return 50;
    50      } else if (x < y) {
    51          return 100;
    52      } else if (x > y) {
    53          return 200;
    54      } else {
    55          return 300;
    56      }
    57  };
    58  assertEqual(test2, [1, 3], 0, 9);
    59  assertEqual(test2, [10, 15], 50, 18);
    60  assertEqual(test2, [60, 70], 100, 21);
    61  assertEqual(test2, [90, 80], 200, 24);
    62  assertEqual(test2, [100, 100], 300, 24);
    63  
    64  // test 3.
    65  var test3 = function (x, y) {
    66      if (x < 5 || x < y) {
    67          return 0;
    68      } else if (x < 50 || x < y) {
    69          return 50;
    70      } else if (x < y) {
    71          return 100;
    72      } else if (x > y) {
    73          return 200;
    74      } else {
    75          return 300;
    76      }
    77  };
    78  assertEqual(test3, [1, 0], 0, 9);
    79  assertEqual(test3, [5, 6], 0, 9);
    80  assertEqual(test3, [10, 11], 0, 9);
    81  assertEqual(test3, [40, 30], 50, 18);
    82  assertEqual(test3, [60, 30], 200, 24);
    83  assertEqual(test3, [60, 60], 300, 24);
    84  
    85  
    86  // test 4.
    87  var test4 = function (x) {
    88      if (x < 5)
    89          return x * 2;
    90      else if (x < 20)
    91          return Math.floor(x / 2);
    92      else if (x < 40)
    93          return x * 3 + 1;
    94      else
    95          return x - 20;
    96  };
    97  assertEqual(test4, [1], 2, 6);
    98  assertEqual(test4, [15], 7, 21);
    99  assertEqual(test4, [30], 91, 15);
   100  assertEqual(test4, [100], 80, 12);