DEV Community

Nicolas DUBIEN
Nicolas DUBIEN

Posted on • Updated on

Advent of PBT 2021 - Day 16 - Solution

Our algorithm was: reversed.
Go to the subject itself for more details

CodeSandbox with a possible set of properties you may have come with: https://codesandbox.io/s/advent-of-pbt-day-16-solution-bywoo?file=/src/index.spec.ts&previewwindow=tests


Property 1: should produce an array having the same length

for any array
it should reverse it to an array having the same length

Written with fast-check:

it("should produce an array having the same length", () => {
  fc.assert(
    fc.property(fc.array(fc.anything()), (data) => {
      // Arrange / Act
      const rev = reversed(data);

      // Assert
      expect(rev).toHaveLength(data.length);
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Property 2: should reverse any array

While this property is definitely the one we would like to define, I tend not to like it as it somehow rewrites the implementation with the same risk of offset by 1. I'd rather go for property 3 to assess that characteristic.

for any array
it should reverse it

Written with fast-check:

it("should reverse any array", () => {
  fc.assert(
    fc.property(fc.array(fc.anything()), (data) => {
      // Arrange / Act
      const rev = reversed(data);

      // Assert
      for (let index = 0; index !== data.length; ++index) {
        expect(rev[rev.length - index - 1]).toBe(data[index]);
      }
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Property 3: should properly reverse concatenated arrays: rev concat(a,b) = concat(rev b, rev a)

This property is rather mathematics but at the end it fulfill the exact same mission as property number 2: checking that the computed array is well reversed. But instead of focusing on each items it focuses on chunks of the array.

for any arrays a and b
we should have: rev concat(a,b) = concat(rev b, rev a)

Written with fast-check:

it("should properly reverse concatenated arrays: rev concat(a,b) = concat(rev b, rev a)", () => {
  fc.assert(
    fc.property(fc.array(fc.anything()), fc.array(fc.anything()), (a, b) => {
      // Arrange / Act
      const rev = reversed([...a, ...b]);
      const revA = reversed(a);
      const revB = reversed(b);

      // Assert
      expect(rev).toEqual([...revB, ...revA]);
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Property 4: should go back to the original array if reversed twice

for any arrays a and b
we should have: rev concat(a,b) = concat(rev b, rev a)

Written with fast-check:

it("should go back to the original array if reversed twice", () => {
  fc.assert(
    fc.property(fc.array(fc.anything()), (data) => {
      // Arrange / Act / Assert
      expect(reversed(reversed(data))).toEqual(data);
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Back to "Advent of PBT 2021" to see topics covered during the other days and their solutions.

More about this serie on @ndubien or with the hashtag #AdventOfPBT.

Top comments (0)