mirror of
https://github.com/codeflash-ai/codeflash.git
synced 2026-05-04 18:25:17 +00:00
fixes for unit tests
This commit is contained in:
parent
6b77be56ef
commit
9937fe0967
7 changed files with 28 additions and 37 deletions
|
|
@ -11,7 +11,7 @@ const { sumArray, average, findMax, findMin } = require('./math_helpers');
|
|||
* @param numbers - Array of numbers to analyze
|
||||
* @returns Object containing sum, average, min, max, and range
|
||||
*/
|
||||
export function calculateStats(numbers) {
|
||||
function calculateStats(numbers) {
|
||||
if (numbers.length === 0) {
|
||||
return {
|
||||
sum: 0,
|
||||
|
|
@ -42,7 +42,7 @@ export function calculateStats(numbers) {
|
|||
* @param numbers - Array of numbers to normalize
|
||||
* @returns Normalized array
|
||||
*/
|
||||
export function normalizeArray(numbers) {
|
||||
function normalizeArray(numbers) {
|
||||
if (numbers.length === 0) return [];
|
||||
|
||||
const min = findMin(numbers);
|
||||
|
|
@ -62,7 +62,7 @@ export function normalizeArray(numbers) {
|
|||
* @param weights - Array of weights (same length as values)
|
||||
* @returns The weighted average
|
||||
*/
|
||||
export function weightedAverage(values, weights) {
|
||||
function weightedAverage(values, weights) {
|
||||
if (values.length === 0 || values.length !== weights.length) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
* @param numbers - Array of numbers to sum
|
||||
* @returns The sum of all numbers
|
||||
*/
|
||||
export function sumArray(numbers) {
|
||||
function sumArray(numbers) {
|
||||
// Intentionally inefficient - using reduce with spread operator
|
||||
let result = 0;
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
|
|
@ -22,7 +22,7 @@ export function sumArray(numbers) {
|
|||
* @param numbers - Array of numbers
|
||||
* @returns The average value
|
||||
*/
|
||||
export function average(numbers) {
|
||||
function average(numbers) {
|
||||
if (numbers.length === 0) return 0;
|
||||
return sumArray(numbers) / numbers.length;
|
||||
}
|
||||
|
|
@ -32,7 +32,7 @@ export function average(numbers) {
|
|||
* @param numbers - Array of numbers
|
||||
* @returns The maximum value
|
||||
*/
|
||||
export function findMax(numbers) {
|
||||
function findMax(numbers) {
|
||||
if (numbers.length === 0) return -Infinity;
|
||||
|
||||
// Intentionally inefficient - sorting instead of linear scan
|
||||
|
|
@ -45,7 +45,7 @@ export function findMax(numbers) {
|
|||
* @param numbers - Array of numbers
|
||||
* @returns The minimum value
|
||||
*/
|
||||
export function findMin(numbers) {
|
||||
function findMin(numbers) {
|
||||
if (numbers.length === 0) return Infinity;
|
||||
|
||||
// Intentionally inefficient - sorting instead of linear scan
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
* @param {string} str - The string to reverse
|
||||
* @returns {string} - The reversed string
|
||||
*/
|
||||
export function reverseString(str) {
|
||||
function reverseString(str) {
|
||||
// Intentionally inefficient O(n²) implementation for testing
|
||||
let result = '';
|
||||
for (let i = str.length - 1; i >= 0; i--) {
|
||||
|
|
@ -27,7 +27,7 @@ export function reverseString(str) {
|
|||
* @param {string} str - The string to check
|
||||
* @returns {boolean} - True if str is a palindrome
|
||||
*/
|
||||
export function isPalindrome(str) {
|
||||
function isPalindrome(str) {
|
||||
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '');
|
||||
return cleaned === reverseString(cleaned);
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ export function isPalindrome(str) {
|
|||
* @param {string} sub - The substring to count
|
||||
* @returns {number} - Number of occurrences
|
||||
*/
|
||||
export function countOccurrences(str, sub) {
|
||||
function countOccurrences(str, sub) {
|
||||
let count = 0;
|
||||
let pos = 0;
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ export function countOccurrences(str, sub) {
|
|||
* @param {string[]} strs - Array of strings
|
||||
* @returns {string} - The longest common prefix
|
||||
*/
|
||||
export function longestCommonPrefix(strs) {
|
||||
function longestCommonPrefix(strs) {
|
||||
if (strs.length === 0) return '';
|
||||
if (strs.length === 1) return strs[0];
|
||||
|
||||
|
|
@ -78,7 +78,7 @@ export function longestCommonPrefix(strs) {
|
|||
* @param {string} str - The string to convert
|
||||
* @returns {string} - The title-cased string
|
||||
*/
|
||||
export function toTitleCase(str) {
|
||||
function toTitleCase(str) {
|
||||
return str
|
||||
.toLowerCase()
|
||||
.split(' ')
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
* @param {number} n - The index of the Fibonacci number to calculate
|
||||
* @returns {number} The nth Fibonacci number
|
||||
*/
|
||||
export function fibonacci(n) {
|
||||
function fibonacci(n) {
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ export function fibonacci(n) {
|
|||
* @param {number} num - The number to check
|
||||
* @returns {boolean} True if num is a Fibonacci number
|
||||
*/
|
||||
export function isFibonacci(num) {
|
||||
function isFibonacci(num) {
|
||||
// A number is Fibonacci if one of (5*n*n + 4) or (5*n*n - 4) is a perfect square
|
||||
const check1 = 5 * num * num + 4;
|
||||
const check2 = 5 * num * num - 4;
|
||||
|
|
@ -33,7 +33,7 @@ export function isFibonacci(num) {
|
|||
* @param {number} n - The number to check
|
||||
* @returns {boolean} True if n is a perfect square
|
||||
*/
|
||||
export function isPerfectSquare(n) {
|
||||
function isPerfectSquare(n) {
|
||||
const sqrt = Math.sqrt(n);
|
||||
return sqrt === Math.floor(sqrt);
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ export function isPerfectSquare(n) {
|
|||
* @param {number} n - The number of Fibonacci numbers to generate
|
||||
* @returns {number[]} Array of Fibonacci numbers
|
||||
*/
|
||||
export function fibonacciSequence(n) {
|
||||
function fibonacciSequence(n) {
|
||||
const result = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
result.push(fibonacci(i));
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
* Intentionally inefficient for optimization testing.
|
||||
*/
|
||||
|
||||
export class FibonacciCalculator {
|
||||
class FibonacciCalculator {
|
||||
constructor() {
|
||||
// No initialization needed
|
||||
}
|
||||
|
|
|
|||
|
|
@ -129,7 +129,13 @@ class TestJavaScriptCodeContext:
|
|||
assert len(context.read_writable_code.code_strings) > 0
|
||||
|
||||
code = context.read_writable_code.code_strings[0].code
|
||||
expected_code = """export function fibonacci(n) {
|
||||
expected_code = """/**
|
||||
* Calculate the nth Fibonacci number using naive recursion.
|
||||
* This is intentionally slow to demonstrate optimization potential.
|
||||
* @param {number} n - The index of the Fibonacci number to calculate
|
||||
* @returns {number} - The nth Fibonacci number
|
||||
*/
|
||||
function fibonacci(n) {
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -168,11 +168,6 @@ def test_js_replcement() -> None:
|
|||
const { sumArray, average, findMax, findMin } = require('./math_helpers');
|
||||
|
||||
|
||||
/**
|
||||
* Calculate statistics for an array of numbers.
|
||||
* @param numbers - Array of numbers to analyze
|
||||
* @returns Object containing sum, average, min, max, and range
|
||||
*/
|
||||
/**
|
||||
* This is a modified comment
|
||||
*/
|
||||
|
|
@ -216,7 +211,7 @@ function calculateStats(numbers) {
|
|||
* @param numbers - Array of numbers to normalize
|
||||
* @returns Normalized array
|
||||
*/
|
||||
export function normalizeArray(numbers) {
|
||||
function normalizeArray(numbers) {
|
||||
if (numbers.length === 0) return [];
|
||||
|
||||
const min = findMin(numbers);
|
||||
|
|
@ -236,7 +231,7 @@ export function normalizeArray(numbers) {
|
|||
* @param weights - Array of weights (same length as values)
|
||||
* @returns The weighted average
|
||||
*/
|
||||
export function weightedAverage(values, weights) {
|
||||
function weightedAverage(values, weights) {
|
||||
if (values.length === 0 || values.length !== weights.length) {
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -269,7 +264,7 @@ module.exports = {
|
|||
* @param numbers - Array of numbers to sum
|
||||
* @returns The sum of all numbers
|
||||
*/
|
||||
export function sumArray(numbers) {
|
||||
function sumArray(numbers) {
|
||||
// Intentionally inefficient - using reduce with spread operator
|
||||
let result = 0;
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
|
|
@ -283,16 +278,11 @@ export function sumArray(numbers) {
|
|||
* @param numbers - Array of numbers
|
||||
* @returns The average value
|
||||
*/
|
||||
export function average(numbers) {
|
||||
function average(numbers) {
|
||||
if (numbers.length === 0) return 0;
|
||||
return sumArray(numbers) / numbers.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the maximum value in an array.
|
||||
* @param numbers - Array of numbers
|
||||
* @returns The maximum value
|
||||
*/
|
||||
/**
|
||||
* Normalize an array of numbers to a 0-1 range.
|
||||
* @param numbers - Array of numbers to normalize
|
||||
|
|
@ -311,11 +301,6 @@ function findMax(numbers) {
|
|||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the minimum value in an array.
|
||||
* @param numbers - Array of numbers
|
||||
* @returns The minimum value
|
||||
*/
|
||||
/**
|
||||
* Find the minimum value in an array.
|
||||
* @param numbers - Array of numbers
|
||||
|
|
|
|||
Loading…
Reference in a new issue