urbit-ob

JavaScript utilities for phonemic base wrangling.
git clone git://git.jtobin.io/urbit-ob.git
Log | Files | Refs | README

co.test.js (7984B)


      1 const BN = require('bn.js')
      2 const { expect } = require('chai')
      3 const jsc = require('jsverify')
      4 const {
      5   patp,
      6   patp2hex,
      7   hex2patp,
      8   patp2dec,
      9   patq,
     10   patq2hex,
     11   hex2patq,
     12   patq2dec,
     13   clan,
     14   sein,
     15   eqPatq,
     16   isValidPatq,
     17   isValidPatp
     18   } = require('../src/internal/co')
     19 
     20 const patps = jsc.uint32.smap(
     21   num => patp(num),
     22   pp => parseInt(patp2dec(pp))
     23 )
     24 
     25 const patqs = jsc.uint32.smap(
     26   num => patq(num),
     27   pq => parseInt(patq2dec(pq))
     28 )
     29 
     30 describe('patp, etc.', () => {
     31   it('patp2dec matches expected reference values', () => {
     32     expect(patp2dec('~zod')).to.equal('0')
     33     expect(patp2dec('~lex')).to.equal('200')
     34     expect(patp2dec('~binzod')).to.equal('512')
     35     expect(patp2dec('~samzod')).to.equal('1024')
     36     expect(patp2dec('~poldec-tonteg')).to.equal('9896704')
     37     expect(patp2dec('~nidsut-tomdun')).to.equal('15663360')
     38     expect(patp2dec('~morlyd-mogmev')).to.equal('3108299008')
     39     expect(patp2dec('~fipfes-morlyd')).to.equal('479733505')
     40   })
     41 
     42   it('patp matches expected reference values', () => {
     43     expect(patp('0')).to.equal('~zod')
     44     expect(patp('200')).to.equal('~lex')
     45     expect(patp('512')).to.equal('~binzod')
     46     expect(patp('1024')).to.equal('~samzod')
     47     expect(patp('9896704')).to.equal('~poldec-tonteg')
     48     expect(patp('15663360')).to.equal('~nidsut-tomdun')
     49     expect(patp('3108299008')).to.equal('~morlyd-mogmev')
     50     expect(patp('479733505')).to.equal('~fipfes-morlyd')
     51   })
     52 
     53   it('large patp values match expected reference values', () => {
     54     expect(hex2patp('7468697320697320736f6d6520766572792068696768207175616c69747920656e74726f7079'))
     55     .to.equal('~divmes-davset-holdet--sallun-salpel-taswet-holtex--watmeb-tarlun-picdet-magmes--holter-dacruc-timdet-divtud--holwet-maldut-padpel-sivtud')
     56   })
     57 
     58   it('patp throws on null input', () => {
     59     let input = () => patp(null)
     60     expect(input).to.throw()
     61   })
     62 
     63   it('hex2patp throws on null input', () => {
     64     let input = () => hex2patp(null)
     65     expect(input).to.throw()
     66   })
     67 
     68   it('patp2hex throws on invalid patp', () => {
     69     let input = () => patp2hex('nidsut-tomdun')
     70     expect(input).to.throw()
     71     input = () => patp2hex('~nidsut-tomdzn')
     72     expect(input).to.throw()
     73     input = () => patp2hex('~sut-tomdun')
     74     expect(input).to.throw()
     75     input = () => patp2hex('~nidsut-dun')
     76     expect(input).to.throw()
     77     input = () => patp2hex(null)
     78     expect(input).to.throw()
     79   })
     80 
     81   it('patp and patp2dec are inverses', () => {
     82     let iso0 = jsc.forall(jsc.uint32, num =>
     83       parseInt(patp2dec(patp(num))) === num
     84     )
     85 
     86     let iso1 = jsc.forall(patps, pp =>
     87       patp(patp2dec(pp)) === pp
     88     )
     89 
     90     jsc.assert(iso0)
     91     jsc.assert(iso1)
     92   })
     93 
     94   it('patp2hex and hex2patp are inverses', () => {
     95     let iso0 = jsc.forall(jsc.uint32, num =>
     96       parseInt(patp2hex(hex2patp(num.toString(16))), 16) === num
     97     )
     98 
     99     let iso1 = jsc.forall(patps, pp =>
    100       hex2patp(patp2hex(pp)) === pp
    101     )
    102 
    103     jsc.assert(iso0)
    104     jsc.assert(iso1)
    105   })
    106 
    107 })
    108 
    109 describe('patq, etc.', () => {
    110   it('patq2dec matches expected reference values', () => {
    111     expect(patq2dec('~zod')).to.equal('0')
    112     expect(patq2dec('~binzod')).to.equal('512')
    113     expect(patq2dec('~samzod')).to.equal('1024')
    114     expect(patq2dec('~poldec-tonteg')).to.equal('4016240379')
    115     expect(patq2dec('~nidsut-tomdun')).to.equal('1208402137')
    116   })
    117 
    118   it('patq matches expected reference values', () => {
    119     expect(patq('0')).to.equal('~zod')
    120     expect(patq('512')).to.equal('~binzod')
    121     expect(patq('1024')).to.equal('~samzod')
    122     expect(patq('4016240379')).to.equal('~poldec-tonteg')
    123     expect(patq('1208402137')).to.equal('~nidsut-tomdun')
    124   })
    125 
    126   it('large patq values match expected reference values', () => {
    127     expect(hex2patq('01010101010101010102')).to.equal('~marnec-marnec-marnec-marnec-marbud')
    128     expect(hex2patq('6d7920617765736f6d65207572626974207469636b65742c206920616d20736f206c75636b79'))
    129     .to.equal('~tastud-holruc-sidwet-salpel-taswet-holdeg-paddec-davdut-holdut-davwex-balwet-divwen-holdet-holruc-taslun-salpel-holtux-dacwex-baltud')
    130   })
    131 
    132   it('patq2hex throws on invalid patp', () => {
    133     let input = () => patq2hex('nidsut-tomdun')
    134     expect(input).to.throw()
    135     input = () => patq2hex('~nidsut-tomdzn')
    136     expect(input).to.throw()
    137     input = () => patq2hex('~sut-tomdun')
    138     expect(input).to.throw()
    139     input = () => patq2hex('~nidsut-dun')
    140     expect(input).to.throw()
    141     input = () => patq2hex(null)
    142     expect(input).to.throw()
    143   })
    144 
    145   it('patq and patq2dec are inverses', () => {
    146     let iso0 = jsc.forall(jsc.uint32, num =>
    147       parseInt(patq2dec(patq(num))) === num
    148     )
    149 
    150     let iso1 = jsc.forall(patqs, pp =>
    151       patq(patq2dec(pp)) === pp
    152     )
    153 
    154     jsc.assert(iso0)
    155     jsc.assert(iso1)
    156   })
    157 
    158   it('patq2hex and hex2patq are inverses', () => {
    159     let iso0 = jsc.forall(jsc.uint32, num =>
    160       parseInt(patq2hex(hex2patq(num.toString(16))), 16) === num
    161     )
    162 
    163     let iso1 = jsc.forall(patqs, pp =>
    164       hex2patq(patq2hex(pp)) === pp
    165     )
    166 
    167     jsc.assert(iso0)
    168     jsc.assert(iso1)
    169   })
    170 })
    171 
    172 describe('clan/sein', () => {
    173   it('clan works as expected', () => {
    174     expect(clan('~zod')).to.equal('galaxy')
    175     expect(clan('~fes')).to.equal('galaxy')
    176     expect(clan('~marzod')).to.equal('star')
    177     expect(clan('~fassec')).to.equal('star')
    178     expect(clan('~dacsem-fipwex')).to.equal('planet')
    179     expect(clan('~fidnum-rosbyt')).to.equal('planet')
    180     expect(clan('~doznec-bannux-nopfen')).to.equal('moon')
    181     expect(clan('~dozryt--wolmep-racmyl-padpeg-mocryp')).to.equal('comet')
    182   })
    183 
    184   it('clan throws on invalid input', () => {
    185     let input = () => clan('~zord')
    186     expect(input).to.throw()
    187     input = () => clan('zod')
    188     expect(input).to.throw()
    189     input = () => clan('~nid-tomdun')
    190     expect(input).to.throw()
    191     input = () => clan(null)
    192     expect(input).to.throw()
    193   })
    194 
    195   it('sein works as expected', () => {
    196     expect(sein('~zod')).to.equal('~zod')
    197     expect(sein('~nec')).to.equal('~nec')
    198     expect(sein('~rep')).to.equal('~rep')
    199     expect(sein('~marzod')).to.equal('~zod')
    200     expect(sein('~marnec')).to.equal('~nec')
    201     expect(sein('~fassec')).to.equal('~sec')
    202     expect(sein('~nidsut-tomdun')).to.equal('~marzod')
    203     expect(sein('~sansym-ribnux')).to.equal('~marnec')
    204   })
    205 
    206   it('sein throws on invalid input', () => {
    207     let input = () => sein('~zord')
    208     expect(input).to.throw()
    209     input = () => sein('zod')
    210     expect(input).to.throw()
    211     input = () => sein('~nid-tomdun')
    212     expect(input).to.throw()
    213     input = () => sein(null)
    214     expect(input).to.throw()
    215   })
    216 
    217 })
    218 
    219 describe('eqPatq', () => {
    220   it('works as expected', () => {
    221     expect(eqPatq('~dozzod-dozzod', '~zod')).to.equal(true)
    222     expect(eqPatq('~dozzod-mardun', '~mardun')).to.equal(true)
    223     expect(eqPatq('~dozzod-mardun', '~mardun-dozzod')).to.equal(false)
    224   })
    225 })
    226 
    227 describe('isValidPat{q, p}', () => {
    228   it('isValidPatp returns true for valid @p values', () => {
    229     expect(isValidPatp('~zod')).to.equal(true)
    230     expect(isValidPatp('~marzod')).to.equal(true)
    231     expect(isValidPatp('~nidsut-tomdun')).to.equal(true)
    232   })
    233 
    234   it('isValidPatp returns false for invalid @p values', () => {
    235     expect(isValidPatp('')).to.equal(false)
    236     expect(isValidPatp('~')).to.equal(false)
    237     expect(isValidPatp('~hu')).to.equal(false)
    238     expect(isValidPatp('~what')).to.equal(false)
    239     expect(isValidPatp('sudnit-duntom')).to.equal(false)
    240   })
    241 
    242   it('isValidPatq returns true for valid @p values', () => {
    243     expect(isValidPatq('~zod')).to.equal(true)
    244     expect(isValidPatq('~marzod')).to.equal(true)
    245     expect(isValidPatq('~nidsut-tomdun')).to.equal(true)
    246     expect(isValidPatq('~dozzod-binwes-nidsut-tomdun')).to.equal(true)
    247   })
    248 
    249   it('isValidPatq returns false for invalid @p values', () => {
    250     expect(isValidPatq('')).to.equal(false)
    251     expect(isValidPatq('~')).to.equal(false)
    252     expect(isValidPatq('~hu')).to.equal(false)
    253     expect(isValidPatq('~what')).to.equal(false)
    254     expect(isValidPatq('sudnit-duntom')).to.equal(false)
    255   })
    256 })