Skip to content

Commit 9937fe0

Browse files
fixes for unit tests
1 parent 6b77be5 commit 9937fe0

7 files changed

Lines changed: 28 additions & 37 deletions

File tree

code_to_optimize/js/code_to_optimize_js/calculator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { sumArray, average, findMax, findMin } = require('./math_helpers');
1111
* @param numbers - Array of numbers to analyze
1212
* @returns Object containing sum, average, min, max, and range
1313
*/
14-
export function calculateStats(numbers) {
14+
function calculateStats(numbers) {
1515
if (numbers.length === 0) {
1616
return {
1717
sum: 0,
@@ -42,7 +42,7 @@ export function calculateStats(numbers) {
4242
* @param numbers - Array of numbers to normalize
4343
* @returns Normalized array
4444
*/
45-
export function normalizeArray(numbers) {
45+
function normalizeArray(numbers) {
4646
if (numbers.length === 0) return [];
4747

4848
const min = findMin(numbers);
@@ -62,7 +62,7 @@ export function normalizeArray(numbers) {
6262
* @param weights - Array of weights (same length as values)
6363
* @returns The weighted average
6464
*/
65-
export function weightedAverage(values, weights) {
65+
function weightedAverage(values, weights) {
6666
if (values.length === 0 || values.length !== weights.length) {
6767
return 0;
6868
}

code_to_optimize/js/code_to_optimize_js/math_helpers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @param numbers - Array of numbers to sum
99
* @returns The sum of all numbers
1010
*/
11-
export function sumArray(numbers) {
11+
function sumArray(numbers) {
1212
// Intentionally inefficient - using reduce with spread operator
1313
let result = 0;
1414
for (let i = 0; i < numbers.length; i++) {
@@ -22,7 +22,7 @@ export function sumArray(numbers) {
2222
* @param numbers - Array of numbers
2323
* @returns The average value
2424
*/
25-
export function average(numbers) {
25+
function average(numbers) {
2626
if (numbers.length === 0) return 0;
2727
return sumArray(numbers) / numbers.length;
2828
}
@@ -32,7 +32,7 @@ export function average(numbers) {
3232
* @param numbers - Array of numbers
3333
* @returns The maximum value
3434
*/
35-
export function findMax(numbers) {
35+
function findMax(numbers) {
3636
if (numbers.length === 0) return -Infinity;
3737

3838
// Intentionally inefficient - sorting instead of linear scan
@@ -45,7 +45,7 @@ export function findMax(numbers) {
4545
* @param numbers - Array of numbers
4646
* @returns The minimum value
4747
*/
48-
export function findMin(numbers) {
48+
function findMin(numbers) {
4949
if (numbers.length === 0) return Infinity;
5050

5151
// Intentionally inefficient - sorting instead of linear scan

code_to_optimize/js/code_to_optimize_js/string_utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @param {string} str - The string to reverse
88
* @returns {string} - The reversed string
99
*/
10-
export function reverseString(str) {
10+
function reverseString(str) {
1111
// Intentionally inefficient O(n²) implementation for testing
1212
let result = '';
1313
for (let i = str.length - 1; i >= 0; i--) {
@@ -27,7 +27,7 @@ export function reverseString(str) {
2727
* @param {string} str - The string to check
2828
* @returns {boolean} - True if str is a palindrome
2929
*/
30-
export function isPalindrome(str) {
30+
function isPalindrome(str) {
3131
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '');
3232
return cleaned === reverseString(cleaned);
3333
}
@@ -38,7 +38,7 @@ export function isPalindrome(str) {
3838
* @param {string} sub - The substring to count
3939
* @returns {number} - Number of occurrences
4040
*/
41-
export function countOccurrences(str, sub) {
41+
function countOccurrences(str, sub) {
4242
let count = 0;
4343
let pos = 0;
4444

@@ -57,7 +57,7 @@ export function countOccurrences(str, sub) {
5757
* @param {string[]} strs - Array of strings
5858
* @returns {string} - The longest common prefix
5959
*/
60-
export function longestCommonPrefix(strs) {
60+
function longestCommonPrefix(strs) {
6161
if (strs.length === 0) return '';
6262
if (strs.length === 1) return strs[0];
6363

@@ -78,7 +78,7 @@ export function longestCommonPrefix(strs) {
7878
* @param {string} str - The string to convert
7979
* @returns {string} - The title-cased string
8080
*/
81-
export function toTitleCase(str) {
81+
function toTitleCase(str) {
8282
return str
8383
.toLowerCase()
8484
.split(' ')

code_to_optimize/js/code_to_optimize_js_cjs/fibonacci.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @param {number} n - The index of the Fibonacci number to calculate
1010
* @returns {number} The nth Fibonacci number
1111
*/
12-
export function fibonacci(n) {
12+
function fibonacci(n) {
1313
if (n <= 1) {
1414
return n;
1515
}
@@ -21,7 +21,7 @@ export function fibonacci(n) {
2121
* @param {number} num - The number to check
2222
* @returns {boolean} True if num is a Fibonacci number
2323
*/
24-
export function isFibonacci(num) {
24+
function isFibonacci(num) {
2525
// A number is Fibonacci if one of (5*n*n + 4) or (5*n*n - 4) is a perfect square
2626
const check1 = 5 * num * num + 4;
2727
const check2 = 5 * num * num - 4;
@@ -33,7 +33,7 @@ export function isFibonacci(num) {
3333
* @param {number} n - The number to check
3434
* @returns {boolean} True if n is a perfect square
3535
*/
36-
export function isPerfectSquare(n) {
36+
function isPerfectSquare(n) {
3737
const sqrt = Math.sqrt(n);
3838
return sqrt === Math.floor(sqrt);
3939
}
@@ -43,7 +43,7 @@ export function isPerfectSquare(n) {
4343
* @param {number} n - The number of Fibonacci numbers to generate
4444
* @returns {number[]} Array of Fibonacci numbers
4545
*/
46-
export function fibonacciSequence(n) {
46+
function fibonacciSequence(n) {
4747
const result = [];
4848
for (let i = 0; i < n; i++) {
4949
result.push(fibonacci(i));

code_to_optimize/js/code_to_optimize_js_cjs/fibonacci_class.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Intentionally inefficient for optimization testing.
44
*/
55

6-
export class FibonacciCalculator {
6+
class FibonacciCalculator {
77
constructor() {
88
// No initialization needed
99
}

tests/test_languages/test_javascript_e2e.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ def test_extract_code_context_for_javascript(self, js_project_dir):
129129
assert len(context.read_writable_code.code_strings) > 0
130130

131131
code = context.read_writable_code.code_strings[0].code
132-
expected_code = """export function fibonacci(n) {
132+
expected_code = """/**
133+
* Calculate the nth Fibonacci number using naive recursion.
134+
* This is intentionally slow to demonstrate optimization potential.
135+
* @param {number} n - The index of the Fibonacci number to calculate
136+
* @returns {number} - The nth Fibonacci number
137+
*/
138+
function fibonacci(n) {
133139
if (n <= 1) {
134140
return n;
135141
}

tests/test_languages/test_multi_file_code_replacer.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,6 @@ def test_js_replcement() -> None:
168168
const { sumArray, average, findMax, findMin } = require('./math_helpers');
169169
170170
171-
/**
172-
* Calculate statistics for an array of numbers.
173-
* @param numbers - Array of numbers to analyze
174-
* @returns Object containing sum, average, min, max, and range
175-
*/
176171
/**
177172
* This is a modified comment
178173
*/
@@ -216,7 +211,7 @@ def test_js_replcement() -> None:
216211
* @param numbers - Array of numbers to normalize
217212
* @returns Normalized array
218213
*/
219-
export function normalizeArray(numbers) {
214+
function normalizeArray(numbers) {
220215
if (numbers.length === 0) return [];
221216
222217
const min = findMin(numbers);
@@ -236,7 +231,7 @@ def test_js_replcement() -> None:
236231
* @param weights - Array of weights (same length as values)
237232
* @returns The weighted average
238233
*/
239-
export function weightedAverage(values, weights) {
234+
function weightedAverage(values, weights) {
240235
if (values.length === 0 || values.length !== weights.length) {
241236
return 0;
242237
}
@@ -269,7 +264,7 @@ def test_js_replcement() -> None:
269264
* @param numbers - Array of numbers to sum
270265
* @returns The sum of all numbers
271266
*/
272-
export function sumArray(numbers) {
267+
function sumArray(numbers) {
273268
// Intentionally inefficient - using reduce with spread operator
274269
let result = 0;
275270
for (let i = 0; i < numbers.length; i++) {
@@ -283,16 +278,11 @@ def test_js_replcement() -> None:
283278
* @param numbers - Array of numbers
284279
* @returns The average value
285280
*/
286-
export function average(numbers) {
281+
function average(numbers) {
287282
if (numbers.length === 0) return 0;
288283
return sumArray(numbers) / numbers.length;
289284
}
290285
291-
/**
292-
* Find the maximum value in an array.
293-
* @param numbers - Array of numbers
294-
* @returns The maximum value
295-
*/
296286
/**
297287
* Normalize an array of numbers to a 0-1 range.
298288
* @param numbers - Array of numbers to normalize
@@ -311,11 +301,6 @@ def test_js_replcement() -> None:
311301
return max;
312302
}
313303
314-
/**
315-
* Find the minimum value in an array.
316-
* @param numbers - Array of numbers
317-
* @returns The minimum value
318-
*/
319304
/**
320305
* Find the minimum value in an array.
321306
* @param numbers - Array of numbers

0 commit comments

Comments
 (0)