From 9937fe0967826cf074d81fa8ab4e52c77b5f303a Mon Sep 17 00:00:00 2001 From: ali Date: Thu, 12 Feb 2026 19:30:46 +0200 Subject: [PATCH] fixes for unit tests --- .../js/code_to_optimize_js/calculator.js | 6 ++--- .../js/code_to_optimize_js/math_helpers.js | 8 +++---- .../js/code_to_optimize_js/string_utils.js | 10 ++++---- .../js/code_to_optimize_js_cjs/fibonacci.js | 8 +++---- .../fibonacci_class.js | 2 +- tests/test_languages/test_javascript_e2e.py | 8 ++++++- .../test_multi_file_code_replacer.py | 23 ++++--------------- 7 files changed, 28 insertions(+), 37 deletions(-) diff --git a/code_to_optimize/js/code_to_optimize_js/calculator.js b/code_to_optimize/js/code_to_optimize_js/calculator.js index cecf92ebb..3eceb7a70 100644 --- a/code_to_optimize/js/code_to_optimize_js/calculator.js +++ b/code_to_optimize/js/code_to_optimize_js/calculator.js @@ -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; } diff --git a/code_to_optimize/js/code_to_optimize_js/math_helpers.js b/code_to_optimize/js/code_to_optimize_js/math_helpers.js index 72a320919..f6e7c9662 100644 --- a/code_to_optimize/js/code_to_optimize_js/math_helpers.js +++ b/code_to_optimize/js/code_to_optimize_js/math_helpers.js @@ -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 diff --git a/code_to_optimize/js/code_to_optimize_js/string_utils.js b/code_to_optimize/js/code_to_optimize_js/string_utils.js index 9c4eb5a04..6881943e5 100644 --- a/code_to_optimize/js/code_to_optimize_js/string_utils.js +++ b/code_to_optimize/js/code_to_optimize_js/string_utils.js @@ -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(' ') diff --git a/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci.js b/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci.js index cdb9bd5f8..17de243bc 100644 --- a/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci.js +++ b/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci.js @@ -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)); diff --git a/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci_class.js b/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci_class.js index 9c816ada0..24621ee7f 100644 --- a/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci_class.js +++ b/code_to_optimize/js/code_to_optimize_js_cjs/fibonacci_class.js @@ -3,7 +3,7 @@ * Intentionally inefficient for optimization testing. */ -export class FibonacciCalculator { +class FibonacciCalculator { constructor() { // No initialization needed } diff --git a/tests/test_languages/test_javascript_e2e.py b/tests/test_languages/test_javascript_e2e.py index ae268def5..017e8f66e 100644 --- a/tests/test_languages/test_javascript_e2e.py +++ b/tests/test_languages/test_javascript_e2e.py @@ -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; } diff --git a/tests/test_languages/test_multi_file_code_replacer.py b/tests/test_languages/test_multi_file_code_replacer.py index b4d2854b6..65f3930e5 100644 --- a/tests/test_languages/test_multi_file_code_replacer.py +++ b/tests/test_languages/test_multi_file_code_replacer.py @@ -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