A Foray Into Testing Algos

It’s kind of a mess and shows no real separation of concerns, even for a personal academic project.
A pretty basic algorithm, but good because it contains some important methods.
  1. Loop through the input array, multiplying the value of each index with itself, Math.pow(<your_value>, multiple), and push that new value to a variable to hold an array of new values, newArr.
  2. This last part, line 34, handles the case where we are working with negative numbers. Since the sort() method increases our solution’s complexity, we want to be sure to only use it when we need it, which is when we have a negative number in our input. And our input is ordered so we can simply just test to see if the value in the first index of our input array is negative, if(array[0] < 1), and if it is, then we sort the array we added all our new values to in the previous part, newArr.sort(a,b) => a-b.
In, out, simple.
O(n log n), the built-in sort will sort our new array in place.
Lines 57 & 58 for the previous example.
There’s a typo, can you find it?
  1. Here we create a function that wraps around all of our tests, and when we run this file with Node this function will execute all our tests on our sortedSquaredArray function.
  2. This is our return. Doesn’t tell us whether it’s correct or not, or even which test the output belongs to.
Your DRY alarm should be going off. Don’t repeat yourself.
  1. I created a function runTest. It will return whether sortedSquaredArray returns the expected value.
  2. runTest will take the input array we want to test as its first argument and the array we expect to be returned and which we previously had commented, as the second argument.
  3. Here we create the function runTest and label the parameters appropriately, input (the array we are testing which will be passed to sortedSquaredArray), and expectedOutput (what we expect to be returned if our solution, sortedSquaredArray is correct).
  4. The runTest function will call our sortedSquaredArray function and store the result in a variable actualOutput. Then we compare actualOutput variable to the expectedOutput argument and return a value conditionally on whether the values match up. That conditional return will be logged to the console.
  5. Not all of our tests have passed.
By storing out tests in an Object they can be referenced with little complexity and identified using the key.
  1. I’m going to put all of our inputs and outputs into an enumerable list. I chose to use an object over an array for this to reduce complexity and because we have a variable number of tests. That is we may add more in the future and it doesn’t matter what order the tests are executed in. Additionally, we can use the key to reference which test produced a particular result.
  2. Here I just test that I am getting the desired output. That is, the message will reference the unique key of the ‘tests’ object as a reference to the test that produced a result and it will also log the actual contents of that test.
  1. We replace the console.log() with a call to our runTest function, passing in the parts of our object, as we enumerate through it. Additionally, I added a third argument which is the object key that I will use as an ID to identify which test we are currently on. We are getting mostly the same information passed into our runTest function so I really only had to alter the messaging to reflect the ID of the test that is currently being performed.
  2. We can now see a console log of each test by its ID, whether it passed the test, and why it failed, if applicable.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Corey Lynch

Corey Lynch

Frontend Software Developer and Security Technician with experience in Ruby, Rails, JavaScript, and React. Flatiron Software Engineering Alumni.