Advent of PBT 2021 - Day 16 - Solution
Tags: challenge, testing, webdev, javascript
Published: 2021-12-16 08:14:04

---
title: Advent of PBT 2021 - Day 16 - Solution
published: true
description: Advent of Property Based Testing 2021 - Day 16 - Solution
tags: challenge,testing,webdev,javascript
//cover_image: https://direct_url_to_image.jpg
---

Our algorithm was: **reversed**.
[Go to the subject itself for more details](https://dev.to/dubzzz/advent-of-pbt-2021-day-16-2hma)

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:

```ts
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);
    })
  );
});
```

---

## 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:

```ts
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]);
      }
    })
  );
});
```

---

## 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:

```ts
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]);
    })
  );
});
```

---

## 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:

```ts
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);
    })
  );
});
```

---

[Back to "Advent of PBT 2021"](https://dev.to/dubzzz/advent-of-pbt-2021-13ee) to see topics covered during the other days and their solutions.

More about this serie on [@ndubien](https://twitter.com/ndubien) or with the hashtag [#AdventOfPBT](https://twitter.com/search?q=%23AdventOfPBT).