|
| 1 | +var Lib = require('@src/lib'); |
| 2 | +var supplyDefaults = require('../assets/supply_defaults'); |
| 3 | +var isTypedArray = require('../../../src/lib/is_array').isTypedArray; |
| 4 | +var b64 = require('base64-arraybuffer'); |
| 5 | +var mock1 = require('@mocks/typed_array_repr_scatter.json'); |
| 6 | + |
| 7 | +var typedArraySpecs = [ |
| 8 | + ['int8', new Int8Array([-128, -34, 1, 127])], |
| 9 | + ['uint8', new Uint8Array([0, 1, 127, 255])], |
| 10 | + ['int16', new Int16Array([-32768, -123, 345, 32767])], |
| 11 | + ['uint16', new Uint16Array([0, 345, 32767, 65535])], |
| 12 | + ['int32', new Int32Array([-2147483648, -123, 345, 32767, 2147483647])], |
| 13 | + ['uint32', new Uint32Array([0, 345, 32767, 4294967295])], |
| 14 | + ['float32', new Float32Array([1.2E-38, -2345.25, 2.7182818, 3.1415926, 2, 3.4E38])], |
| 15 | + ['float64', new Float64Array([5.0E-324, 2.718281828459045, 3.141592653589793, 1.8E308])] |
| 16 | +]; |
| 17 | + |
| 18 | +describe('Test TypedArray representations', function() { |
| 19 | + 'use strict'; |
| 20 | + |
| 21 | + describe('ArrayBuffer', function() { |
| 22 | + it('should accept representation as ArrayBuffer', function() { |
| 23 | + typedArraySpecs.forEach(function(arraySpec) { |
| 24 | + // Build data and confirm its type |
| 25 | + var data = arraySpec[1].buffer; |
| 26 | + expect(data.constructor).toEqual(ArrayBuffer); |
| 27 | + |
| 28 | + var repr = { |
| 29 | + dtype: arraySpec[0], |
| 30 | + data: data |
| 31 | + }; |
| 32 | + var gd = { |
| 33 | + data: [{ |
| 34 | + y: repr |
| 35 | + }], |
| 36 | + }; |
| 37 | + |
| 38 | + supplyDefaults(gd); |
| 39 | + |
| 40 | + expect(gd.data[0].y).toEqual(repr); |
| 41 | + expect(gd._fullData[0].y).toEqual(arraySpec[1]); |
| 42 | + }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('Array', function() { |
| 47 | + it('should accept representation as Array', function() { |
| 48 | + typedArraySpecs.forEach(function(arraySpec) { |
| 49 | + // Build data and confirm its type |
| 50 | + var data = Array.prototype.slice.call(arraySpec[1]); |
| 51 | + expect(Array.isArray(data)).toEqual(true); |
| 52 | + |
| 53 | + var repr = { |
| 54 | + dtype: arraySpec[0], |
| 55 | + data: data |
| 56 | + }; |
| 57 | + var gd = { |
| 58 | + data: [{ |
| 59 | + y: repr |
| 60 | + }], |
| 61 | + }; |
| 62 | + |
| 63 | + supplyDefaults(gd); |
| 64 | + |
| 65 | + expect(gd.data[0].y).toEqual(repr); |
| 66 | + expect(gd._fullData[0].y).toEqual(arraySpec[1]); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('DataView', function() { |
| 72 | + it('should accept representation as DataView', function() { |
| 73 | + typedArraySpecs.forEach(function(arraySpec) { |
| 74 | + // Build data and confirm its type |
| 75 | + var data = new DataView(arraySpec[1].buffer); |
| 76 | + expect(data.constructor).toEqual(DataView); |
| 77 | + |
| 78 | + var repr = { |
| 79 | + dtype: arraySpec[0], |
| 80 | + data: data |
| 81 | + }; |
| 82 | + var gd = { |
| 83 | + data: [{ |
| 84 | + y: repr |
| 85 | + }], |
| 86 | + }; |
| 87 | + |
| 88 | + supplyDefaults(gd); |
| 89 | + |
| 90 | + expect(gd.data[0].y).toEqual(repr); |
| 91 | + expect(gd._fullData[0].y).toEqual(arraySpec[1]); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('base64', function() { |
| 97 | + it('should accept representation as base 64 string', function() { |
| 98 | + typedArraySpecs.forEach(function(arraySpec) { |
| 99 | + // Build data and confirm its type |
| 100 | + var data = b64.encode(arraySpec[1].buffer); |
| 101 | + expect(typeof data).toEqual('string'); |
| 102 | + |
| 103 | + var repr = { |
| 104 | + dtype: arraySpec[0], |
| 105 | + data: data |
| 106 | + }; |
| 107 | + var gd = { |
| 108 | + data: [{ |
| 109 | + y: repr |
| 110 | + }], |
| 111 | + }; |
| 112 | + |
| 113 | + supplyDefaults(gd); |
| 114 | + expect(gd.data[0].y).toEqual(repr); |
| 115 | + expect(gd._fullData[0].y).toEqual(arraySpec[1]); |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('mock', function() { |
| 121 | + it('should accept representation as base 64 and Array in Mock', function() { |
| 122 | + |
| 123 | + var gd = Lib.extendDeep({}, mock1); |
| 124 | + supplyDefaults(gd); |
| 125 | + |
| 126 | + // Check x |
| 127 | + // data_array property |
| 128 | + expect(gd.data[0].x).toEqual({ |
| 129 | + 'dtype': 'float64', |
| 130 | + 'data': [3, 2, 1]}); |
| 131 | + expect(gd._fullData[0].x).toEqual(new Float64Array([3, 2, 1])); |
| 132 | + |
| 133 | + // Check y |
| 134 | + // data_array property |
| 135 | + expect(gd.data[0].y).toEqual({ |
| 136 | + 'dtype': 'float32', |
| 137 | + 'data': 'AABAQAAAAEAAAIA/'}); |
| 138 | + expect(gd._fullData[0].y).toEqual(new Float32Array([3, 2, 1])); |
| 139 | + |
| 140 | + // Check marker.color |
| 141 | + // This is an arrayOk property not a data_array property |
| 142 | + expect(gd.data[0].marker.color).toEqual({ |
| 143 | + 'dtype': 'uint16', |
| 144 | + 'data': 'AwACAAEA'}); |
| 145 | + expect(gd._fullData[0].marker.color).toEqual(new Uint16Array([3, 2, 1])); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments