import { ARRAY_START_TOKEN , ARRAY_END_TOKEN , STRING_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_MANY_TOKEN , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , OBJECT_END_TOKEN , TEMPLATE_OBJECT_START , TEMPLATE_OBJECT_END , TEMPLATE_OBJECT_FINAL , UNDEFINED_TOKEN , INTEGER_SMALL_TOKENS , INTEGER_SMALL_TOKEN_ELEMENT_OFFSET } from '../../src/constants' ; import { expect } from 'chai' ; import { describe , it } from 'mocha' ; import { TestCase } from './util' ; const testCases = [ new TestCase ( 'empty' , [ ] , [ ARRAY_START_TOKEN , ARRAY_END_TOKEN ] ) , new TestCase ( 'single-number' , [ 1 ] , [ ARRAY_START_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , ARRAY_END_TOKEN ] ) , new TestCase ( 'two-numbers' , [ 1 , 2 ] , [ ARRAY_START_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , ARRAY_END_TOKEN ] ) , new TestCase ( 'one-string' , [ 'hello' ] , [ ARRAY_START_TOKEN , STRING_TOKEN , 'hello' , STRING_TOKEN , ARRAY_END_TOKEN ] ) , new TestCase ( 'two-strings' , [ 'hello' , 'yello' ] , [ ARRAY_START_TOKEN , STRING_TOKEN , 'hello' , STRING_TOKEN , STRING_TOKEN , 'yello' , STRING_TOKEN , ARRAY_END_TOKEN ] ) , new TestCase ( 'repeated-once' , [ 1 , 1 ] , [ ARRAY_START_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , ARRAY_REPEAT_TOKEN , ARRAY_END_TOKEN ] ) , new TestCase ( 'repeated-five' , [ 1 , 1 , 1 , 1 , 1 ] , [ ARRAY_START_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_MANY_TOKEN , '1' , ARRAY_END_TOKEN ] ) , new TestCase ( 'repeated-not-repeated-repeated' , [ 1 , 1 , 2 , 1 , 1 ] , [ ARRAY_START_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , ARRAY_REPEAT_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , ARRAY_REPEAT_TOKEN , ARRAY_END_TOKEN ] ) , new TestCase ( 'not-repeated-repeated' , [ 1 , 2 , 2 ] , [ ARRAY_START_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , ARRAY_REPEAT_TOKEN , ARRAY_END_TOKEN ] ) , new TestCase ( 'repeated-repeated-many-repeated-not-repeated' , [ 1 , 1 , 2 , 2 , 2 , 2 , 2 , 3 ] , [ ARRAY_START_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , ARRAY_REPEAT_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_MANY_TOKEN , '1' , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 3 ] , ARRAY_END_TOKEN ] ) , new TestCase ( 'one-object' , [ { x : 1 } ] , [ ARRAY_START_TOKEN , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , OBJECT_END_TOKEN , ARRAY_END_TOKEN , ] ) , new TestCase ( 'two-objects' , [ { x : 1 } , { y : 1 } ] , [ ARRAY_START_TOKEN , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , OBJECT_END_TOKEN , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , 'y' , UNREFERENCED_STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , OBJECT_END_TOKEN , ARRAY_END_TOKEN , ] ) , new TestCase ( 'one-object-one-string-one-number' , [ { x : 1 } , 'hello' , 2 ] , [ ARRAY_START_TOKEN , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , OBJECT_END_TOKEN , STRING_TOKEN , 'hello' , STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , ARRAY_END_TOKEN , ] ) , new TestCase ( 'shallow-template-object' , [ { x : 1 } , { x : 2 } ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , TEMPLATE_OBJECT_FINAL , ARRAY_END_TOKEN , ] ) , new TestCase ( 'shallow-template-object-non-object' , [ { x : 1 } , { x : 2 } , 3 ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , TEMPLATE_OBJECT_FINAL , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 3 ] , ARRAY_END_TOKEN , ] ) , new TestCase ( 'shallow-template-object-non-template-object' , [ { x : 1 } , { x : 2 } , { y : 3 } ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , TEMPLATE_OBJECT_FINAL , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , 'y' , UNREFERENCED_STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 3 ] , OBJECT_END_TOKEN , ARRAY_END_TOKEN , ] ) , new TestCase ( 'shallow-template-object-repeated' , [ { x : 1 } , { x : 2 } , { x : 2 } , { x : 2 } , { x : 2 } , { x : 2 } ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_MANY_TOKEN , '1' , TEMPLATE_OBJECT_FINAL , ARRAY_END_TOKEN , ] ) , new TestCase ( 'deep-template-object' , [ { x : 1 , y : { l : { k : 2 , s : 3 } } , z : 4 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 'y' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'l' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'k' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 's' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , TEMPLATE_OBJECT_END , UNREFERENCED_STRING_TOKEN , 'z' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 3 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 4 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 5 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 6 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 7 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 8 ] , TEMPLATE_OBJECT_FINAL , ARRAY_END_TOKEN , ] ) , new TestCase ( 'deep-template-object-similar-object' , [ { x : 1 , y : { l : { k : 2 , s : 3 } } , z : 4 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } , { x : 9 , y : { l : - 1 } } ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 'y' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'l' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'k' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 's' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , TEMPLATE_OBJECT_END , UNREFERENCED_STRING_TOKEN , 'z' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 3 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 4 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 5 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 6 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 7 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 8 ] , TEMPLATE_OBJECT_FINAL , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 9 ] , UNREFERENCED_STRING_TOKEN , 'y' , UNREFERENCED_STRING_TOKEN , OBJECT_START_TOKEN , UNREFERENCED_STRING_TOKEN , 'l' , UNREFERENCED_STRING_TOKEN , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET - 1 ] , OBJECT_END_TOKEN , OBJECT_END_TOKEN , ARRAY_END_TOKEN , ] ) , new TestCase ( 'deep-template-object-non-object' , [ { x : 1 , y : { l : { k : 2 , s : 3 } } , z : 4 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } , 9 ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 'y' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'l' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'k' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 's' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , TEMPLATE_OBJECT_END , UNREFERENCED_STRING_TOKEN , 'z' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 3 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 4 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 5 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 6 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 7 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 8 ] , TEMPLATE_OBJECT_FINAL , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 9 ] , ARRAY_END_TOKEN , ] ) , new TestCase ( 'deep-template-object-repeated' , [ { x : 1 , y : { l : { k : 2 , s : 3 } } , z : 4 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } , { x : 5 , y : { l : { k : 6 , s : 7 } } , z : 8 } , ] , [ ARRAY_START_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'x' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 'y' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'l' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_START , UNREFERENCED_STRING_TOKEN , 'k' , UNREFERENCED_STRING_TOKEN , UNREFERENCED_STRING_TOKEN , 's' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , TEMPLATE_OBJECT_END , UNREFERENCED_STRING_TOKEN , 'z' , UNREFERENCED_STRING_TOKEN , TEMPLATE_OBJECT_END , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 1 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 2 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 3 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 4 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 5 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 6 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 7 ] , INTEGER_SMALL_TOKENS [ INTEGER_SMALL_TOKEN_ELEMENT_OFFSET + 8 ] , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_TOKEN , ARRAY_REPEAT_MANY_TOKEN , '1' , TEMPLATE_OBJECT_FINAL , ARRAY_END_TOKEN , ] ) , ] ; describe ( 'Array' , function ( ) { for ( let i = 0 ; i < testCases . length ; i ++ ) { const testCase = testCases [ i ] ; describe ( testCase . name , function ( ) { it ( 'compress' , function ( ) { testCase . compress ( ) ; } ) ; it ( 'decompress' , function ( ) { testCase . decompress ( ) ; } ) ; it ( 'decompress-incremental' , function ( ) { testCase . decompressIncremental ( ) ; } ) ; } ) ; } } ) ;