简朴易用的leetcode开辟测试东西(npm)

形貌

近来在用es6解leetcode,当题目比较复杂时,有能够修改了新的毛病,却影响了前面的流程。要用通用的测试东西,却又有杀鸡用牛刀的觉得,所以就写了个简朴易用的leetcode开辟测试东西,分享与人人。

东西装置

npm i leetcode_test

运用示例1 (题目010)

codes:

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:

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

运用示例2 (题目015)

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给我,我会尽快处置惩罚。

    原文作者:zhoutk
    原文地址: https://segmentfault.com/a/1190000017356892
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞