Skip to content

Commit

Permalink
Merge signed and unsigned integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Sep 4, 2024
1 parent ac0b148 commit 16f58c8
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 110 deletions.
5 changes: 4 additions & 1 deletion packages/app/src/metadata-viewer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
isH5WebComplex,
isIntegerType,
isNumericType,
isScalarShape,
} from '@h5web/shared/guards';
Expand Down Expand Up @@ -29,7 +30,9 @@ export function renderShape(shape: Shape): string {
export function renderType(type: DType): string {
if (isNumericType(type)) {
const { endianness, size } = type;
return `${type.class}, ${size}-bit${endianness ? `, ${endianness}` : ''}`;
const signStr = isIntegerType(type) && !type.signed ? ' (unsigned)' : '';
const endiannessStr = endianness ? `, ${endianness}` : '';
return `${type.class}${signStr}, ${size}-bit${endiannessStr}`;
}

if (type.class === DTypeClass.String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 8,
},
"value": -128,
Expand All @@ -35,6 +36,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 8,
},
"value": Int8Array [
Expand All @@ -59,6 +61,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 16,
},
"value": -32768,
Expand All @@ -79,6 +82,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 16,
},
"value": Int16Array [
Expand All @@ -103,6 +107,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 32,
},
"value": -2147483648,
Expand All @@ -120,6 +125,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "big-endian",
"signed": true,
"size": 32,
},
"value": 0,
Expand All @@ -140,6 +146,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 32,
},
"value": Int32Array [
Expand All @@ -164,6 +171,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"value": -9223372036854776000,
Expand All @@ -184,6 +192,7 @@ exports[`test file matches snapshot 1`] = `
"type": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"value": [
Expand All @@ -206,8 +215,9 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 8,
},
"value": 255,
Expand All @@ -226,8 +236,9 @@ exports[`test file matches snapshot 1`] = `
3,
],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 8,
},
"value": Uint8Array [
Expand All @@ -250,8 +261,9 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 16,
},
"value": 65535,
Expand All @@ -270,8 +282,9 @@ exports[`test file matches snapshot 1`] = `
3,
],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 16,
},
"value": Uint16Array [
Expand All @@ -294,8 +307,9 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 32,
},
"value": 4294967295,
Expand All @@ -314,8 +328,9 @@ exports[`test file matches snapshot 1`] = `
3,
],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 32,
},
"value": Uint32Array [
Expand All @@ -338,8 +353,9 @@ exports[`test file matches snapshot 1`] = `
},
"shape": [],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 64,
},
"value": 18446744073709552000,
Expand All @@ -359,8 +375,9 @@ exports[`test file matches snapshot 1`] = `
2,
],
"type": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 64,
},
"value": [
Expand Down Expand Up @@ -1255,6 +1272,7 @@ exports[`test file matches snapshot 1`] = `
"bigint": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"double": {
Expand Down Expand Up @@ -1318,6 +1336,7 @@ exports[`test file matches snapshot 1`] = `
"bigint": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"double": {
Expand Down Expand Up @@ -1428,6 +1447,7 @@ exports[`test file matches snapshot 1`] = `
"bigint": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"bool": {
Expand Down Expand Up @@ -1518,8 +1538,9 @@ exports[`test file matches snapshot 1`] = `
},
"vlen": {
"base": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 64,
},
"class": "Array (variable length)",
Expand Down Expand Up @@ -1714,8 +1735,9 @@ exports[`test file matches snapshot 1`] = `
"shape": [],
"type": {
"base": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 8,
},
"class": "Enumeration",
Expand Down Expand Up @@ -1749,6 +1771,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 32,
},
"class": "Enumeration",
Expand Down Expand Up @@ -1783,8 +1806,9 @@ exports[`test file matches snapshot 1`] = `
],
"type": {
"base": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 8,
},
"class": "Enumeration",
Expand Down Expand Up @@ -1832,8 +1856,9 @@ exports[`test file matches snapshot 1`] = `
],
"type": {
"base": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 8,
},
"class": "Enumeration",
Expand Down Expand Up @@ -1885,6 +1910,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 8,
},
"class": "Array (variable length)",
Expand Down Expand Up @@ -1946,6 +1972,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"class": "Array (variable length)",
Expand Down
7 changes: 3 additions & 4 deletions packages/app/src/providers/h5grove/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
referenceType,
strType,
timeType,
uintType,
unknownType,
} from '@h5web/shared/hdf5-utils';
import { describe, expect, it } from 'vitest';
Expand All @@ -23,10 +22,10 @@ import { parseDType } from './utils';
describe('parseDType', () => {
it('should convert integer types', () => {
expect(parseDType({ class: 0, size: 1, order: 0, sign: 1 })).toStrictEqual(
intType(8, H5T_ORDER.LE),
intType(true, 8, H5T_ORDER.LE),
);
expect(parseDType({ class: 0, size: 8, order: 1, sign: 0 })).toStrictEqual(
uintType(64, H5T_ORDER.BE),
intType(false, 64, H5T_ORDER.BE),
);
});

Expand Down Expand Up @@ -83,7 +82,7 @@ describe('parseDType', () => {
base: { class: 0, size: 4, order: 0, sign: 0 },
members: { FOO: 41, BAR: 42 },
}),
).toStrictEqual(enumType(uintType(), { FOO: 41, BAR: 42 }));
).toStrictEqual(enumType(intType(false), { FOO: 41, BAR: 42 }));

expect(
parseDType({
Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/providers/h5grove/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNumericType } from '@h5web/shared/guards';
import { H5T_CLASS } from '@h5web/shared/h5t';
import { H5T_CLASS, H5T_SIGN } from '@h5web/shared/h5t';
import type {
Attribute,
ChildEntity,
Expand All @@ -15,7 +15,7 @@ import {
compoundOrCplxType,
enumOrBoolType,
floatType,
intOrUintType,
intType,
opaqueType,
referenceType,
strType,
Expand Down Expand Up @@ -149,7 +149,7 @@ export function parseDType(type: H5GroveType): DType {
const { class: h5tClass, size } = type;

if (h5tClass === H5T_CLASS.INTEGER) {
return intOrUintType(type.sign, size * 8, type.order);
return intType(type.sign === H5T_SIGN.SIGN_2, size * 8, type.order);
}

if (h5tClass === H5T_CLASS.FLOAT) {
Expand Down
5 changes: 2 additions & 3 deletions packages/app/src/providers/hsds/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
floatType,
intType,
strType,
uintType,
unknownType,
} from '@h5web/shared/hdf5-utils';
import { describe, expect, it } from 'vitest';
Expand All @@ -29,12 +28,12 @@ interface TestType {

const leInt = {
hsds: { class: 'H5T_INTEGER', base: 'H5T_STD_I8LE' },
hdf5: intType(8, H5T_ORDER.LE),
hdf5: intType(true, 8, H5T_ORDER.LE),
} satisfies TestType;

const beUint = {
hsds: { class: 'H5T_INTEGER', base: 'H5T_STD_U64BE' },
hdf5: uintType(64, H5T_ORDER.BE),
hdf5: intType(false, 64, H5T_ORDER.BE),
} satisfies TestType;

const leFloat = {
Expand Down
7 changes: 1 addition & 6 deletions packages/app/src/providers/hsds/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
floatType,
intType,
strType,
uintType,
unknownType,
} from '@h5web/shared/hdf5-utils';

Expand Down Expand Up @@ -88,11 +87,7 @@ function convertHsdsNumericType(hsdsType: HsdsNumericType): NumericType {
return floatType(size, h5tOrder);
}

if (sign === 'U') {
return uintType(size, h5tOrder);
}

return intType(size, h5tOrder);
return intType(sign === 'I', size, h5tOrder);
}

function convertHsdsCompoundType(
Expand Down
Loading

0 comments on commit 16f58c8

Please sign in to comment.