Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Tfjs Generic Utils Test

From Leeroopedia
Revision as of 16:51, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_Generic_Utils_Test.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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 conditions
  • count - Counting occurrences in string and number arrays
  • Compare functions - numberCompare and reverseNumberCompare for sorting
  • toList - Converting values to arrays
  • toSnakeCase - camelCase to snake_case conversion
  • toCamelCase - snake_case to camelCase conversion
  • stringsEqual - Array string equality (order-independent)
  • unique - Extracting unique elements from arrays
  • isObjectEmpty - Checking if an object has no keys
  • checkArrayTypeAndLength - Validating array element types and lengths
  • formatValueAsFriendlyString - Human-readable value formatting
  • debounce - Function debouncing with delay
  • getCartesianProductOfValues - 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 not
  • count(['foo', 'bar', 'foo'], 'foo') returns 2
  • toSnakeCase('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

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment