Matlab: Improper index matrix reference (or outsmarting matlab)

前端 未结 5 2258
梦谈多话
梦谈多话 2021-02-09 16:47

I want to be able to write jasmine-like tests in Matlab. So something like

expect(myfibonacci(0)).toBe(0);
expect(myfibonacci(5)).toBe(15);
expect(myfibonacci(10         


        
5条回答
  •  走了就别回头了
    2021-02-09 17:08

    In the latest versions of MATLAB (13a/13b) there's a unit testing framework built in that looks very similar to what you're attempting. Instead of

    expect(myfibonacci(0)).toBe(0);
    

    you would write

    import matlab.unittest.constraints.IsEqualTo
    testCase.verifyThat(myfibonacci(0), IsEqualTo(0))
    

    (You could also/instead have assumeThat, assertThat, or fatalAssertThat).

    If for some reason you wish to implement your own framework, note the small difference in your syntaxes - you have a dot whereas MathWorks have a comma between myfibonacci(0) and the test condition.

    In MATLAB you can't index into the result of a subscripted expression like that (well, you could, but you would have to overload subsref, and that's a world of pain, trust me). So the way they've done it is to introduce the test comparison conditions as a separate package, and apply them as a separate input argument rather than as a method with the dot syntax.

    Take a look at the documentation for the new unit testing framework to find out more about either the framework itself, or (if you'd prefer to roll your own) the syntaxes they have designed as a comparison to yours.

提交回复
热议问题