unicornpaint

A web-based painting app for raspberry PI and pimoroni Unicorn Hat HD
Log | Files | Refs | README

Utils.test.js (3484B)


      1 import { colorEqual, coordsEqual, xy, rgb, findContiguousPixels, rotatePixelsClock, rotatePixelsCounterClock } from './Utils'
      2 
      3 test('test colorEqual function', () => {
      4     expect(colorEqual([0, 0, 0], [0, 0, 0]))
      5         .toBe(true)
      6     expect(colorEqual([1, 0, 0], [0, 0, 0]))
      7         .toBe(false)
      8     expect(colorEqual([0, 1, 0], [0, 0, 0]))
      9         .toBe(false)
     10     expect(colorEqual([0, 0, 1], [0, 0, 0]))
     11         .toBe(false)
     12 
     13     expect(colorEqual([0, 0, 1], {r: 0, g: 0, b: 1}))
     14         .toBe(true)
     15     expect(colorEqual({r: 0, g: 0, b: 1}, [0, 0, 1]))
     16         .toBe(true)
     17 
     18     expect(colorEqual({r: 0, g: 0, b: 0}, [0, 0, 1]))
     19         .toBe(false)
     20     expect(colorEqual([0, 0, 1], {r: 0, g: 0, b: 0}))
     21         .toBe(false)
     22 
     23     expect(colorEqual(undefined, {r: 0, g: 0, b: 0}))
     24         .toBe(false)
     25     expect(colorEqual({r: 0, g: 0, b: 0}, undefined))
     26         .toBe(false)
     27 })
     28 
     29 test('coordEqual function', () => {
     30     expect(coordsEqual([0, 0], [0, 0]))
     31         .toBe(true)
     32     expect(coordsEqual([0, 1], [0, 0]))
     33         .toBe(false)
     34     expect(coordsEqual([1, 0], [0, 0]))
     35         .toBe(false)
     36 
     37     expect(coordsEqual([1, 0], [0, 0]))
     38         .toBe(false)
     39 
     40     expect(coordsEqual(undefined, [0, 0]))
     41         .toBe(false)
     42     expect(coordsEqual([0, 0], undefined))
     43         .toBe(false)
     44 })
     45 
     46 test('contiguousPixels', () => {
     47     let allBlack = [
     48         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
     49         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
     50         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
     51         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
     52     ]
     53 
     54     let contiguousPx = findContiguousPixels(0, 0, allBlack)
     55     expect(contiguousPx.length)
     56         .toBe(16)
     57 
     58     expect(findContiguousPixels(0, 0, [
     59         [[0, 0, 0], [0, 0, 1], [0, 0, 0], [0, 0, 0]],
     60         [[0, 0, 1], [0, 0, 1], [0, 0, 0], [0, 0, 0]],
     61         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
     62         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
     63     ]).length)
     64         .toBe(1)
     65 
     66     expect(findContiguousPixels(1, 1, [
     67         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
     68         [[0, 0, 0], [0, 0, 1], [0, 0, 1], [0, 0, 0]],
     69         [[0, 0, 0], [0, 0, 1], [0, 0, 1], [0, 0, 0]],
     70         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
     71     ]).length)
     72         .toBe(4)
     73 
     74     expect(findContiguousPixels(1, 1, [
     75         [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]],
     76         [[0, 0, 1], [0, 0, 0], [0, 0, 0], [0, 0, 1]],
     77         [[0, 0, 1], [0, 0, 0], [0, 0, 0], [0, 0, 1]],
     78         [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]]
     79     ]).length)
     80         .toBe(4)
     81 })
     82 
     83 test('rotate clockwise function', () => {
     84     let rot1 = rotatePixelsClock([
     85         [[0, 0, 1], [0, 0, 0], [0, 0, 0], [0, 0, 2]],
     86         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
     87         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
     88         [[0, 0, 4], [0, 0, 0], [0, 0, 0], [0, 0, 3]]
     89     ])
     90     expect(rot1[0][3][2])
     91     .toBe(1)
     92     expect(rot1[3][3][2])
     93     .toBe(2)
     94     expect(rot1[3][0][2])
     95     .toBe(3)
     96     expect(rot1[0][0][2])
     97     .toBe(4)
     98 })
     99 
    100 test('rotate counter-clockwise function', () => {
    101     let rot1 = rotatePixelsCounterClock([
    102         [[0, 0, 1], [0, 0, 0], [0, 0, 0], [0, 0, 2]],
    103         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
    104         [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
    105         [[0, 0, 4], [0, 0, 0], [0, 0, 0], [0, 0, 3]]
    106     ])
    107     expect(rot1[0][3][2])
    108     .toBe(3)
    109     expect(rot1[3][3][2])
    110     .toBe(4)
    111     expect(rot1[3][0][2])
    112     .toBe(1)
    113     expect(rot1[0][0][2])
    114     .toBe(2)
    115 })