Skip to content

Commit

Permalink
additional test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pattasai committed Feb 14, 2023
1 parent 5504079 commit 18a15e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
25 changes: 12 additions & 13 deletions packages/@aws-cdk/core/lib/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { Token } from './token';
*/
export class Size {
/**
* Create a Storage representing an amount kibibytes.
* 1 KiB = 1024 bytes
* Create a Storage representing an amount bytes.
*
* @param amount the amount of kibibytes to be represented
* @param amount the amount of bytes to be represented
*
* @returns a new `Size` instance
*/
Expand Down Expand Up @@ -200,14 +199,14 @@ export interface SizeConversionOptions {
}

class StorageUnit {
public static readonly Kibibytes = new StorageUnit('kibibytes', 1);
public static readonly Bytes = new StorageUnit('bytes', 1 / 1024);
public static readonly Mebibytes = new StorageUnit('mebibytes', 1024);
public static readonly Gibibytes = new StorageUnit('gibibytes', 1024 * 1024);
public static readonly Tebibytes = new StorageUnit('tebibytes', 1024 * 1024 * 1024);
public static readonly Pebibytes = new StorageUnit('pebibytes', 1024 * 1024 * 1024 * 1024);

private constructor(public readonly label: string, public readonly inKibiBytes: number) {
public static readonly Bytes = new StorageUnit('bytes', 1);
public static readonly Kibibytes = new StorageUnit('kibibytes', 1024);
public static readonly Mebibytes = new StorageUnit('mebibytes', 1024 * 1024);
public static readonly Gibibytes = new StorageUnit('gibibytes', 1024 * 1024 * 1024);
public static readonly Tebibytes = new StorageUnit('tebibytes', 1024 * 1024 * 1024 * 1024);
public static readonly Pebibytes = new StorageUnit('pebibytes', 1024 * 1024 * 1024 * 1024 * 1024);

private constructor(public readonly label: string, public readonly inBytes: number) {
// MAX_SAFE_INTEGER is 2^53, so by representing storage in kibibytes,
// the highest storage we can represent is 8 exbibytes.
}
Expand All @@ -219,12 +218,12 @@ class StorageUnit {

function convert(amount: number, fromUnit: StorageUnit, toUnit: StorageUnit, options: SizeConversionOptions = {}) {
const rounding = options.rounding ?? SizeRoundingBehavior.FAIL;
if (fromUnit.inKibiBytes === toUnit.inKibiBytes) { return amount; }
if (fromUnit.inBytes === toUnit.inBytes) { return amount; }
if (Token.isUnresolved(amount)) {
throw new Error(`Size must be specified as 'Size.${toUnit}()' here since its value comes from a token and cannot be converted (got Size.${fromUnit})`);
}

const multiplier = fromUnit.inKibiBytes / toUnit.inKibiBytes;
const multiplier = fromUnit.inBytes / toUnit.inBytes;
const value = amount * multiplier;
switch (rounding) {
case SizeRoundingBehavior.NONE:
Expand Down
19 changes: 14 additions & 5 deletions packages/@aws-cdk/core/test/size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ describe('size', () => {
);
});

test('Size in bytes', () => {
const size = Size.bytes(1_099_511_627_776);

expect(size.toBytes()).toEqual(1_099_511_627_776);
expect(size.toKibibytes()).toEqual(1_073_741_824);
expect(size.toMebibytes()).toEqual(1_048_576);
expect(size.toGibibytes()).toEqual(1024);
expect(size.toTebibytes()).toEqual(1);
expect(() => size.toPebibytes()).toThrow(/'1099511627776 bytes' cannot be converted into a whole number/);
floatEqual(size.toPebibytes({ rounding: SizeRoundingBehavior.NONE }), 1_099_511_627_776 / (1024 * 1024 * 1024 * 1024 * 1024));

expect(Size.bytes(4 * 1024 * 1024 * 1024).toGibibytes()).toEqual(4);
});

test('Size in kibibytes', () => {
const size = Size.kibibytes(4_294_967_296);

Expand All @@ -41,11 +55,6 @@ describe('size', () => {
expect(Size.mebibytes(4 * 1024).toGibibytes()).toEqual(4);
});

test('Size in bytes test', () => {
const size = Size.bytes(1_073_741_823);
expect(size.toBytes()).toEqual(1_073_741_823);

});

test('Size in gibibyte', () => {
const size = Size.gibibytes(5);
Expand Down

0 comments on commit 18a15e2

Please sign in to comment.