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 Jan 20, 2025
1 parent 14ca931 commit bd16535
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 126 deletions.
11 changes: 10 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,15 @@ 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 endiannessStr = endianness ? `, ${endianness}` : '';
const signStr = isIntegerType(type)
? type.signed
? ' (signed)'
: ' (unsigned)'
: '';

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,12 +1447,14 @@ exports[`test file matches snapshot 1`] = `
"bigint": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"bool": {
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 8,
},
"class": "Boolean",
Expand Down Expand Up @@ -1523,8 +1544,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 @@ -1611,8 +1633,9 @@ exports[`test file matches snapshot 1`] = `
"shape": null,
"type": {
"base": {
"class": "Integer (unsigned)",
"class": "Integer",
"endianness": "little-endian",
"signed": false,
"size": 8,
},
"class": "Boolean",
Expand Down Expand Up @@ -1642,6 +1665,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 8,
},
"class": "Boolean",
Expand Down Expand Up @@ -1671,6 +1695,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 8,
},
"class": "Boolean",
Expand Down Expand Up @@ -1703,6 +1728,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 8,
},
"class": "Boolean",
Expand Down Expand Up @@ -1739,8 +1765,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 @@ -1774,6 +1801,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 32,
},
"class": "Enumeration",
Expand Down Expand Up @@ -1808,8 +1836,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 @@ -1857,8 +1886,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 @@ -1910,6 +1940,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 @@ -1971,6 +2002,7 @@ exports[`test file matches snapshot 1`] = `
"base": {
"class": "Integer",
"endianness": "little-endian",
"signed": true,
"size": 64,
},
"class": "Array (variable length)",
Expand Down
9 changes: 4 additions & 5 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 All @@ -92,7 +91,7 @@ describe('parseDType', () => {
base: { class: 0, size: 1, order: 0, sign: 1 },
members: { FALSE: 0, TRUE: 1 },
}),
).toStrictEqual(boolType(intType(8)));
).toStrictEqual(boolType(intType(true, 8)));
});

it('should convert array types', () => {
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,
type ChildEntity,
Expand All @@ -16,7 +16,7 @@ import {
compoundOrCplxType,
enumOrBoolType,
floatType,
intOrUintType,
intType,
opaqueType,
referenceType,
strType,
Expand Down Expand Up @@ -162,7 +162,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
Loading

0 comments on commit bd16535

Please sign in to comment.