Skip to content

Commit e462b5d

Browse files
committed
Add unit tests for conversion
1 parent 06a3577 commit e462b5d

File tree

3 files changed

+108
-16
lines changed

3 files changed

+108
-16
lines changed

redisinsight/ui/src/utils/formatters/bufferFormatters.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,53 @@ const ASCIIToBuffer = (strInit: string) => {
107107
return anyToBuffer(Array.from(Buffer.from(result, 'hex')))
108108
}
109109

110+
const float32ArrayToBuffer = (vector: Float32Array) => {
111+
const buffer = new ArrayBuffer(npVector.length * 4)
112+
const dataView = new DataView(buffer)
113+
114+
for(let i = 0; i < npVector.length; i++) {
115+
dataView.setFloat32(i * 4, npVector[i], true)
116+
}
117+
118+
return new Uint8Array(buffer)
119+
}
120+
121+
122+
const float64ArrayToBuffer = (vector: Float64Array) => {
123+
const buffer = new ArrayBuffer(npVector.length * 8)
124+
const dataView = new DataView(buffer)
125+
126+
for(let i = 0; i < npVector.length; i++) {
127+
dataView.setFloat64(i * 8, npVector[i], true)
128+
}
129+
130+
return new Uint8Array(buffer)
131+
}
132+
133+
134+
const bufferToFloat32Array = (data: Uint8Array) => {
135+
const buffer = new Uint8Array(data).buffer
136+
const dataView = new DataView(buffer)
137+
let vector = []
138+
139+
for(let i = 0; i < dataView.byteLength; i += 4) {
140+
vector.push(dataView.getFloat32(i, true))
141+
}
142+
return new Float32Array(vector)
143+
}
144+
145+
const bufferToFloat64Array = (data: Uint8Array) => {
146+
const buffer = new Uint8Array(data).buffer
147+
const dataView = new DataView(buffer)
148+
const vector = []
149+
150+
for (let i = 0; i < dataView.byteLength; i += 8) {
151+
vector.push(dataView.getFloat64(i, true))
152+
}
153+
return new Float64Array(vector)
154+
}
155+
156+
110157
const bufferToUint8Array = (reply: RedisResponseBuffer): Uint8Array => new Uint8Array(reply.data)
111158
const bufferToUTF8 = (reply: RedisResponseBuffer): string => decoder.decode(bufferToUint8Array(reply))
112159

@@ -186,7 +233,11 @@ export {
186233
anyToBuffer,
187234
bufferToBinary,
188235
binaryToBuffer,
189-
bufferToJava
236+
bufferToJava,
237+
bufferToFloat32Array,
238+
bufferToFloat64Array,
239+
float32ArrayToBuffer,
240+
float64ArrayToBuffer,
190241
}
191242

192243
window.ri = {

redisinsight/ui/src/utils/formatters/valueFormatters.tsx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
stringToBuffer,
2222
binaryToBuffer,
2323
Maybe,
24+
bufferToFloat64Array,
25+
bufferToFloat32Array,
2426
} from 'uiSrc/utils'
2527
import { reSerializeJSON } from 'uiSrc/utils/formatters/json'
2628

@@ -104,29 +106,18 @@ const formattingBuffer = (
104106
}
105107
case KeyValueFormat.Vector32Bit: {
106108
try {
107-
let buffer = new Uint8Array(reply.data).buffer;
108-
let dataView = new DataView(buffer);
109-
let npVector = [];
110-
111-
for(let i = 0; i < dataView.byteLength; i+=4) {
112-
npVector.push(dataView.getFloat32(i, true));
113-
}
114-
const value = JSONBigInt.stringify(npVector)
109+
const vector = Array.from(bufferToFloat32Array(reply.data))
110+
const value = JSONBigInt.stringify(vector)
115111
return JSONViewer({ value, ...props })
116112
} catch (e) {
117113
return { value: bufferToUTF8(reply), isValid: false }
118114
}
119115
}
120116
case KeyValueFormat.Vector64Bit: {
121117
try {
122-
let buffer = new Uint8Array(reply.data).buffer;
123-
let dataView = new DataView(buffer);
124-
let npVector = [];
118+
const vector = Array.from(bufferToFloat64Array(reply.data))
119+
const value = JSONBigInt.stringify(vector)
125120

126-
for(let i = 0; i < dataView.byteLength; i+=8) {
127-
npVector.push(dataView.getFloat64(i, true));
128-
}
129-
const value = JSONBigInt.stringify(npVector)
130121
return JSONViewer({ value, ...props })
131122
} catch (e) {
132123
return { value: bufferToUTF8(reply), isValid: false }
@@ -173,6 +164,8 @@ const bufferToSerializedFormat = (
173164
case KeyValueFormat.HEX: return bufferToHex(value)
174165
case KeyValueFormat.Binary: return bufferToBinary(value)
175166
case KeyValueFormat.JSON: return reSerializeJSON(bufferToUTF8(value), space)
167+
case KeyValueFormat.Vector32Bit: return bufferToFloat32Array(value.data)
168+
case KeyValueFormat.Vector64Bit: return bufferToFloat64Array(value.data)
176169
case KeyValueFormat.Msgpack: {
177170
try {
178171
const decoded = decode(Uint8Array.from(value.data))

redisinsight/ui/src/utils/tests/formatters/valueFormatters.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,54 @@ describe('bufferToSerializedFormat', () => {
2222
})
2323
})
2424

25+
describe(KeyValueFormat.Vector32Bit, () => {
26+
describe('should properly serialize', () => {
27+
const testValues = [new Float32Array([1.0, 2.0]), new Float32Array([12.12, 13.41])].map((v) => {
28+
input: anyToBuffer(v.buffer)
29+
expected: JSON.stringify(v)
30+
})
31+
32+
test.each(testValues)('test %j', ({input, expected }) => {
33+
expect(JSON.stringify(bufferToSerializedFormat(KeyValueFormat.Vector32Bit, input))).toEqual(expected)
34+
})
35+
})
36+
37+
describe('should properly return value with invalid values', () => {
38+
const testValues = [new Float32Array(['test'])].map((v) => {
39+
input: anyToBuffer(v.buffer)
40+
expected: JSON.stringify(v)
41+
})
42+
43+
test.each(testValues)('test %j', ({input, expected }) => {
44+
expect(JSON.stringify(bufferToSerializedFormat(KeyValueFormat.Vector32Bit, input))).toEqual(expected)
45+
})
46+
})
47+
})
48+
49+
describe(KeyValueFormat.Vector64Bit, () => {
50+
describe('should properly serialize', () => {
51+
const testValues = [new Float64Array([1.0, 2.0]), new Float64Array([12.12, 13.41])].map((v) => {
52+
input: anyToBuffer(v.buffer)
53+
expected: JSON.stringify(v)
54+
})
55+
56+
test.each(testValues)('test %j', ({input, expected }) => {
57+
expect(JSON.stringify(bufferToSerializedFormat(KeyValueFormat.Vector64Bit, input))).toEqual(expected)
58+
})
59+
})
60+
61+
describe('should properly return value with invalid values', () => {
62+
const testValues = [new Float64Array(['test'])].map((v) => {
63+
input: anyToBuffer(v.buffer)
64+
expected: JSON.stringify(v)
65+
})
66+
67+
test.each(testValues)('test %j', ({input, expected }) => {
68+
expect(JSON.stringify(bufferToSerializedFormat(KeyValueFormat.Vector64Bit, input))).toEqual(expected)
69+
})
70+
})
71+
})
72+
2573
describe(KeyValueFormat.Msgpack, () => {
2674
describe('should properly serialize', () => {
2775
const testValues = [{}, '""', 6677, true, { a: { b: [1, 2, '3'] } }].map((v) => ({

0 commit comments

Comments
 (0)