最近在用es6解leetcode,當問題比較複雜時,有可能修正了新的錯誤,卻影響了前面的流程。要用通用的測試工具,卻又有殺雞用牛刀的感受,因此就寫了個簡單易用的leetcode開發測試工具,分享與你們。git
npm i leetcode_testes6
codes:github
let test = require('leetcode_test').test /** * @param {string} s * @param {string} p * @return {boolean} */ var isMatch = function (s, p) { if (p.length === 0) { return s.length === 0 } firstMath = s.length > 0 && (p[0] === s[0] || p[0] === '.') if (p.length >= 2 && p[1] === '*') { //下面兩部分的順序不能交換 return firstMath && isMatch(s.substring(1), p) || isMatch(s, p.substring(2)) } else { return firstMath && isMatch(s.substring(1), p.substring(1)) } }; let cases = [ // [[[],''],], //第一個參數是空數組 [['abbabaaaaaaacaa', 'a*.*b.a.*c*b*a*c*'], true], [['aaa', 'a*ac'], true], //故意寫錯答案,展現測試失敗輸出效果 [['a', '..*'], true], ] test(isMatch, cases)
測試用例編寫說明
leetcode要測試的都是函數,參數個數不定,但返回值是一個。所以,我設計用例的輸入形式爲一個用例就是一個兩個元素的數組,第一個元素是一個數組:對應輸入參數;第二個元素是一個值。
上面例子的輸入參數是([2, 7, 11, 15], 91),第一個參數是數組,第二個參數是數值;返回值是一個數組([0, 1])。 若是要測試的函數的輸入參數就是一個數組,要注意輸入形式,好比,求[1,2,3,4]平均值,要這樣輸入測試用例: [[[1,2,3,4]],2.5]算法
out:npm
test [1] success, Input: ('abbabaaaaaaacaa','a*.*b.a.*c*b*a*c*'); Expected: true; Output: true test [2] fail, Input: ('aaa','a*ac'); Expected: true; Output: false test [3] success, Input: ('a','..*'); Expected: true; Output: true Result: test 3 cases, success: 2, fail: 1 running 5 ms
codes:數組
let test = require('leetcode_test').test /** * @param {number[]} nums * @return {number[][]} */ var threeSum = function (nums) { nums = nums.sort((a,b) => a - b); const rs = []; let i = 0; while (i < nums.length) { let one = nums[i]; let two = i + 1; //從隊列頭部開始 let three = nums.length - 1; //從隊列尾部開始 while (two < three) { let sum = one + nums[two] + nums[three]; if (sum === 0) { rs.push([one,nums[two],nums[three]]); two++; three--; while (two < three && nums[two] === nums[two - 1]) { two++; } while (two < three && nums[three] === nums[three + 1]) { three--; } } else if (sum > 0) three--; else two++; } i++; while (i < nums.length && nums[i] === nums[i - 1]) i++; } return rs; }; let cases = [ // [[[],''],], //第一個參數是空數組 [[[]],[]], [[[1,-1,-1,0]],[-1,0,1]], [[[-1,0,1,0]],[[-1,0,1]]], [[[0,0,0,0]],[0,0,0]], [[[-1,2,-1]],[-1,-1,2]], [[[0,0,0]],[0,0,0]], [[[-1,0,1,2,-1,-4]],[[-1,-1,2],[-1,0,1]]], //answer's sequence is not important [[[-1,0,1,2,-1,-4]],[[-1,0,1],[-1,-1,2]]], //answer's sequence is not important [[[-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]],[[-4,-2,6],[-4,0,4],[-4,1,3],[-4,2,2],[-2,-2,4],[-2,0,2]]], [[[-4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0]],[[-5,1,4],[-4,0,4],[-4,1,3],[-2,-2,4],[-2,1,1],[0,0,0]]], ] test(threeSum,cases)
測試用例編寫說明
測試用例的7與8,期待結果的數組元素順序並不影響答案的斷定。函數
out:工具
test [1] success, Input: ([]); Expected: []; Output: [] test [2] success, Input: ([-1,-1,0,1]); Expected: [-1,0,1]; Output: [[-1,0,1]] test [3] success, Input: ([-1,0,0,1]); Expected: [[-1,0,1]]; Output: [[-1,0,1]] test [4] success, Input: ([0,0,0,0]); Expected: [0,0,0]; Output: [[0,0,0]] test [5] success, Input: ([-1,-1,2]); Expected: [-1,-1,2]; Output: [[-1,-1,2]] test [6] success, Input: ([0,0,0]); Expected: [0,0,0]; Output: [[0,0,0]] test [7] success, Input: ([-4,-1,-1,0,1,2]); Expected: [[-1,-1,2],[-1,0,1]]; Output: [[-1,-1,2],[-1,0,1]] test [8] success, Input: ([-4,-1,-1,0,1,2]); Expected: [[-1,-1,2],[-1,0,1]]; Output: [[-1,-1,2],[-1,0,1]] test [9] success, Input: ([-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]); Expected: [[-2,-2,4],[-2,0,2],[-4,-2,6],[-4,0,4],[-4,1,3],[-4,2,2]]; Output: [[-2,-2,4],[-2,0,2],[-4,-2,6],[-4,0,4],[-4,1,3],[-4,2,2]] test [10] success, Input: ([-5,-5,-4,-4,-4,-2,-2,-2,0,0,0,1,1,3,4,4]); Expected: [[-2,-2,4],[-2,1,1],[-4,0,4],[-4,1,3],[-5,1,4],[0,0,0]]; Output: [[-2,-2,4],[-2,1,1],[-4,0,4],[-4,1,3],[-5,1,4],[0,0,0]] Result: test 10 cases, success: 10, fail: 0
工具地址:https://github.com/zhoutk/lee...
解答地址:https://github.com/zhoutk/lee...測試
最近一直在用,已經把輸出的樣子調得還能看過眼了,答案對比算法,也改進了。遇到問題,我會持續改進,你們遇到問題也可提bug給我,我會盡快處理。ui