November 21, 2018
The day started with a watching a video with the apprentice group, today’s video was about software craftsmanship by Sandro Mancuso https://www.youtube.com/watch?v=9OhXqBlCmrM, the video is mostly elaborates on some sections of Sandro’s book Software craftsmanship. Once that was over I went on to working through another exercise from learn-c-the-hard-way which covered headers, object files, and shared variables, and the video also touched on creating a state object and passing a reference to the state to functions (like redux) in-order to reduce the amount of potential bugs when threading.
Surprisingly it wasn’t as complicated as it sounds,
// ex22.h
// extern int THE_SIZE;
struct State {
static int the_age;
int the_size;
}
int get_age(struct State *state);
// ex22.c
#include <ex22.h>
// int THE_SIZE = 32;
// static INT THE_AGE = 37;
int get_age(struct State *state)
{
// return THE_AGE
return state->the_age;
}
void set_age(struct State *state, int age)
{
// THE_AGE - age
state->the_age = age;
}
After listening to Masha talk about property based testing, I open some time thinking about a use case for testing functions with randomly generated inputs and recalled a kata I was working on in which one of the functions purpose was to return two words are different from each other by just one letter. JsVerify seems to be a popular library to use for this (although the documentation could be better) I manged to get it working :)
// function to test
// src/diffrentByOneLetter
function diffrentByOneLetter(str1, str2) {
if(str1.length !== str2.length) { return false; }
const count = str1.length;
for(let i = 0; i < str1.length; i += 1) {
if(str1[i] === str2[i]) { count -= 1; }
}
return (count === 1);
}
// The test
// test/prop-test.js
// describe and it are gloabls when using mocha
describe('Testing diffrentByOneLetter, by generating random single characters', () => {
it("Should only return true if there is a one letter diffrence", () => {
return jsc.assertForall(jsc.char, jsc.char, (a, b) => {
// console.log(a, b);
return (a !== b) === diffrentByOneLetter(a, b);
});
});
});
I worked deceptively surprisingly quickly so I had to be certain it worked by logging out put from and sure enough random charters where generated :)
Returning to one of yesterdays topics I wondered if mutation testing would also work and get some hands on experience with stryker.
git branch mutant
git checkout mutant
npm install --save-dev stryker stryker-api
npx stryker init
### select options as needed
npx styker run
### or for more info
# npx stykery run --logLevel=debug
Weirdly enough the file that i used property testing on was the only one that got 100% coverage in the mutation test. Well it’s good to know the the property based testing worked, but most of the other files did not score well at all. You can check out the mutant branch here https://github.com/MarcMcIntosh/word-chain-kata/tree/mutant
Written by Marc McIntosh Find him on github