Implementation:Tensorflow Tfjs Generic Utils Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Layers_API |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
This test suite validates the generic utility functions used throughout the TensorFlow.js Layers codebase. These utilities handle common tasks like array replication, assertions, element counting, comparison functions, list conversion, string case conversion (snake_case to camelCase and vice versa), array equality checking, uniqueness detection, object emptiness checking, array type validation, value formatting, debouncing, and Cartesian product generation. These are foundational helpers that support the entire Layers API infrastructure.
Code Reference
Source Location: tfjs-layers/src/utils/generic_utils_test.ts (369 lines)
Repository: GitHub
Test Describe Blocks
pyListRepeat()- Replicating values/arrays N times (empty, single, multiple, negative)assert- Assertion for false/null/undefined/true conditionscount- Counting occurrences in string and number arraysCompare functions-numberCompareandreverseNumberComparefor sortingtoList- Converting values to arraystoSnakeCase- camelCase to snake_case conversiontoCamelCase- snake_case to camelCase conversionstringsEqual- Array string equality (order-independent)unique- Extracting unique elements from arraysisObjectEmpty- Checking if an object has no keyscheckArrayTypeAndLength- Validating array element types and lengthsformatValueAsFriendlyString- Human-readable value formattingdebounce- Function debouncing with delaygetCartesianProductOfValues- Generating all combinations of input arrays
I/O Contract
Inputs to tests:
- Primitive values, arrays (string[], number[]), and objects
- String values in various cases (camelCase, snake_case)
- Numeric arrays for sorting and counting
- Timer-based inputs for debounce tests
Expected outputs/assertions:
pyListRepeat('a', 3)returns['a', 'a', 'a']assert(false)throws error,assert(true)does notcount(['foo', 'bar', 'foo'], 'foo')returns 2toSnakeCase('fooBar')returns'foo_bar'unique([1, 2, 2, 3])returns[1, 2, 3]getCartesianProductOfValues([1, 2], ['a', 'b'])returns all 4 combinations
Usage Example
describe('pyListRepeat() ', () => {
it('creates an empty array for 0 numValues', () => {
expect(utils.pyListRepeat(null, 0)).toEqual([]);
});
it('creates an array with 3 values for 3 numValues', () => {
const value = 'a';
expect(utils.pyListRepeat(value, 3)).toEqual(['a', 'a', 'a']);
});
it('throws an exception when numValues <0', () => {
expect(() => utils.pyListRepeat(null, -1)).toThrowError();
});
});
describe('count', () => {
it('string array, non-empty', () => {
const array: string[] = ['foo', 'bar', 'foo'];
expect(utils.count(array, 'foo')).toEqual(2);
expect(utils.count(array, 'baz')).toEqual(0);
});
});
Test Coverage Summary
| Category | Count | Details |
|---|---|---|
| Array Utilities | 8+ | pyListRepeat, toList, unique, count |
| String Utilities | 6+ | toSnakeCase, toCamelCase, stringsEqual |
| Validation | 4+ | assert, checkArrayTypeAndLength, isObjectEmpty |
| Sorting | 2 | numberCompare, reverseNumberCompare |
| Async Utilities | 2+ | debounce with timing |
| Combinatorics | 2+ | getCartesianProductOfValues |
| Test Environment | Standard | describe
|