import { OpenApi } from "@typia/interface"; import { _randomMultiple } from "./_randomMultiple"; /** * Generate a random integer from an integer schema. * * Bounds are tightened to the integers they contain, a missing side defaults to * a window of one hundred beside the other, and a step is satisfied through the * decimal multiple generator. * * The optional source supplies draws in [0, 1); it defaults to the platform * source resolved when this helper is called. Nested draws use the same * source. * * @evidence contracts/common.md#principled-implementation The bounds are tightened to the integers they contain, an exclusive bound moves to the next representable integer, a missing side defaults to a window of one hundred around the other, an infinite bound that excludes nothing is dropped and one that excludes everything throws; the scalar draw maps the source fraction with floor(source() * (maximum - minimum + 1)) + minimum when the width is finite, or weighted finite endpoints otherwise, and clamps rounding to the selected bounds, or selects a multiple through the decimal multiple generator. Uniform index probabilities on exactly representable integer intervals require a uniform source; intervals beyond exact integer precision can return only representable numbers. An injected deterministic source retains the same bound arithmetic. * @evidence contracts/common.md#clear-and-simple-design One function with private boundary helpers shared in shape with the number generator. * @evidence contracts/common.md#prohibited-implementation-shortcuts Errors are explicit, and the default window of one hundred is an arbitrary generator choice that is documented by its use only. * @evidence contracts/common.md#meaningful-documentation The doc states how the bounds, the missing side and the step are handled, and the boundary helper explains the infinite bound rule. */ export const _randomInteger = ( schema: OpenApi.IJsonSchema.IInteger, source: () => number = Math.random, ): number => { const lower: IBoundary | null = getLowerBoundary(schema); const upper: IBoundary | null = getUpperBoundary(schema); const minimum: number = lower?.value ?? (upper === null ? 0 : upper.value - 100); const maximum: number = upper?.value ?? (lower === null ? 100 : lower.value + 100); if (minimum > maximum) throw new Error("Minimum value is greater than maximum value."); return schema.multipleOf === undefined ? scalar({ minimum, maximum }, source) : _randomMultiple( { minimum, maximum, multipleOf: schema.multipleOf, exclusiveMinimum: lower?.exclusive ?? false, exclusiveMaximum: upper?.exclusive ?? false, integer: true, }, source, ); }; const scalar = ( props: { minimum: number; maximum: number }, source: () => number, ): number => { const minimum: number = Math.ceil(props.minimum); const maximum: number = Math.floor(props.maximum); if (minimum > maximum) throw new Error("The integer range is empty."); if (!Number.isFinite(minimum) || !Number.isFinite(maximum)) throw new Error("The integer range has no finite value."); const draw: number = source(); const width: number = maximum - minimum + 1; const value: number = Number.isFinite(width) ? Math.floor(draw * width) + minimum : Math.floor((1 - draw) * minimum + draw * maximum); return Math.max(minimum, Math.min(maximum, value)); }; const getLowerBoundary = ( schema: OpenApi.IJsonSchema.IInteger, ): IBoundary | null => { const inclusive: IBoundary | null = boundary(schema.minimum, false, "lower"); const exclusive: IBoundary | null = boundary( schema.exclusiveMinimum, true, "lower", ); const selected: IBoundary | null = selectBoundary( inclusive, exclusive, Math.max, ); if (selected === null) return null; const integer: number = selected.exclusive ? Math.floor(selected.value) + 1 : Math.ceil(selected.value); return { value: selected.exclusive && integer <= selected.value ? nextRepresentable(selected.value, true) : integer, exclusive: false, }; }; const getUpperBoundary = ( schema: OpenApi.IJsonSchema.IInteger, ): IBoundary | null => { const inclusive: IBoundary | null = boundary(schema.maximum, false, "upper"); const exclusive: IBoundary | null = boundary( schema.exclusiveMaximum, true, "upper", ); const selected: IBoundary | null = selectBoundary( inclusive, exclusive, Math.min, ); if (selected === null) return null; const integer: number = selected.exclusive ? Math.ceil(selected.value) - 1 : Math.floor(selected.value); return { value: selected.exclusive && integer >= selected.value ? nextRepresentable(selected.value, false) : integer, exclusive: false, }; }; /** * Reads one bound, which may be infinite. * * A type tag built from an overflowing literal (`Maximum<1e400>`) carries an * infinite bound (#2452). `-Infinity` below or `Infinity` above excludes no * finite number, so it is no bound at all; `Infinity` below or `-Infinity` * above excludes every finite number, so no value can be generated. */ const boundary = ( value: number | undefined, exclusive: boolean, side: "lower" | "upper", ): IBoundary | null => { if (value === undefined) return null; if (Number.isFinite(value)) return { value, exclusive }; if (value === (side === "lower" ? -Infinity : Infinity)) return null; throw new Error("Numeric range has no finite value."); }; const selectBoundary = ( x: IBoundary | null, y: IBoundary | null, compare: (x: number, y: number) => number, ): IBoundary | null => { if (x === null) return y; if (y === null) return x; if (x.value === y.value) return { value: x.value, exclusive: x.exclusive || y.exclusive }; return compare(x.value, y.value) === x.value ? x : y; }; interface IBoundary { value: number; exclusive: boolean; } /** * Advances one IEEE 754 binary64 bit pattern, including signed zero and * subnormals. */ const nextRepresentable = (value: number, upward: boolean): number => { if (value === 0) return upward ? Number.MIN_VALUE : -Number.MIN_VALUE; const view: DataView = new DataView(new ArrayBuffer(8)); view.setFloat64(0, value); const bits: bigint = view.getBigUint64(0); view.setBigUint64(0, bits + (value > 0 === upward ? BigInt(1) : -BigInt(1))); return view.getFloat64(0); };