Ad
  • Custom User Avatar

    Would you mind explaining to me why the incrementer is necessary? I attempted your solution without the incrementer and it did not go well for me. I'm a bit stuck trying to understand what's going on. Thank you in advance.

  • Custom User Avatar

    Awesome thanks!

  • Custom User Avatar

    Oh sorry, I just realize this is a Ruby kata and not JS >3<

    Ruby random test cases are like this:

    r = Random.new # You'd want this to provide randomness
    def sol(a,b,c)
      #reference solution
    end
    
    describe 'Random tests' do
      it 'These are random tests' do
        50.times do # 50 random tests
          a,b,c = # generate inputs
          Test.assert_equals(user_solution(n), sol(n))
        end
      end
    end
    

    Equality in Ruby are deep comparison by default so you don't need to worry about those stuff in JS, though you still need to watch out for input mutation, especially since stuff that aren't mutable in JS can be mutable in Ruby (e.g String).

  • Custom User Avatar

    Lol I'm loving you both :))

  • Custom User Avatar

    Thank you!! I really appreciate the great information. I was wondering how people managed to get 100 or more tests into their katas :D I'll work through what you have suggested to fix my kata, allthough I'll probably have some questions :)

  • Custom User Avatar

    The CW Github wiki still doesn't have a page for writing random tests, so I'll just write a short explanation here:

    You need to generate some random inputs and compare them to the expected answer (usually computed by a reference solution put inside the test fixture. Don't put it in Preloaded!):

    function sol(n) { // this is your reference solution
      /* code */
    }
    
    Test.describe('Random tests', function() {
      Test.it('Should work for random tests', function() {
        for(let i=0; i<100; i++) { // 100 random tests
          let input = generateInput(); // generate input here
          let expected = sol(n);
          let actual = luckyNumber(n); // user solution
          Test.assertEquals(actual, expected);
        }
      });
    });
    

    Some other things to note:

    • If you're passing in objects/arrays to user function, you must run reference solution before user solution (or make a deep clone of the object before passing in), or otherwise user can mutate the object to mess with the tests
    • You should add Object.freeze(Math) in Preloaded to prevent people hacking Math.random and sabotage random test generation
    • If the output is an object/array you should use Test.assertDeepEquals instead of Test.assertEquals to do comparison. And never use Test.expect unless really necessary.
  • Custom User Avatar

    How do I add those? Thanks!

  • Custom User Avatar

    Yes actually I was going to begin with this simple input kata then increase the difficulty of the next by changing the input to an actual date (July 24th, 1995) for instance. Even though this is a digital root exercise, it has a different spin on it and it is not a terrible thing for beginner programmers to get more practice solving these kinds of problems. I actually appreciated being able to practice concepts over and see if I could write the code better with each one when I started programming :)

    Thanks for all the feedback everyone! ;)

  • Custom User Avatar

    Yes, you are right;-), haven't found it...

  • Custom User Avatar

    No random tests

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    So congrats to your first published kata;-)! Wait and see what others say:-)... i'm often too friendly:-P...

  • Custom User Avatar

    Lol, my first one. Should I leave as is?

  • Custom User Avatar

    Looks like a duplicate, but only found some similar ones;-)...

  • Loading more items...