|
| 1 | +// warning: This file is auto generated by `yarn build:tests` |
| 2 | +// Do not edit by hand! |
| 3 | + |
| 4 | +import { createRequire } from 'node:module' |
| 5 | +import { describe, it, expect } from 'vitest' |
| 6 | + |
| 7 | +process.env.TZ = 'UTC' |
| 8 | +const __locutus_source_fn = require('../../../../src/python/functools/reduce.ts').reduce |
| 9 | +const __locutus_source_module_url = new URL("../../../../src/python/functools/reduce.ts", import.meta.url) |
| 10 | +const __locutus_source_require = createRequire(__locutus_source_module_url) |
| 11 | +const __locutus_func_name = "reduce" |
| 12 | +const __locutus_module_js_code = "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.reduce = reduce;\nconst _functools_ts_1 = require(\"../_helpers/_functools.ts\");\nfunction reduce(func, iterable, initializer) {\n // discuss at: https://locutus.io/python/functools/reduce/\n // parity verified: Python 3.12\n // original by: Kevin van Zonneveld (https://kvz.io)\n // note 1: Reduces Python iterables to one plain value using a caller-provided combining function.\n // example 1: reduce((a, b) => a + b, [1, 2, 3, 4])\n // returns 1: 10\n // example 2: reduce((a, b) => a + b, 'abc')\n // returns 2: 'abc'\n return arguments.length >= 3 ? (0, _functools_ts_1.pythonReduce)(func, iterable, initializer) : (0, _functools_ts_1.pythonReduce)(func, iterable, _functools_ts_1.noInitializer);\n}" |
| 13 | +const __locutus_standalone_ts_code = "// python/_helpers/_functools (Locutus helper dependency)\nconst noInitializer = Symbol('noInitializer');\nfunction pythonReduce(func, iterable, initializer = noInitializer) {\n const iterator = toIterator(iterable, 'reduce');\n let accumulator;\n if (initializer !== noInitializer) {\n accumulator = initializer;\n }\n else {\n const first = iterator.next();\n if (first.done) {\n throw new TypeError('reduce() of empty sequence with no initial value');\n }\n accumulator = first.value;\n }\n while (true) {\n const next = iterator.next();\n if (next.done) {\n return accumulator;\n }\n accumulator = func(accumulator, next.value);\n }\n}\nfunction toIterator(iterable, functionName) {\n if (typeof iterable === 'string') {\n return iterable[Symbol.iterator]();\n }\n if ((typeof iterable === 'object' || typeof iterable === 'function') && iterable !== null) {\n const iterableValue = iterable;\n const iteratorFactory = iterableValue[Symbol.iterator];\n if (typeof iteratorFactory === 'function') {\n return iteratorFactory.call(iterableValue);\n }\n }\n throw new TypeError(`${functionName}() expected an iterable`);\n}\n// python/functools/reduce (target function module)\nfunction reduce(func, iterable, initializer) {\n // discuss at: https://locutus.io/python/functools/reduce/\n // parity verified: Python 3.12\n // original by: Kevin van Zonneveld (https://kvz.io)\n // note 1: Reduces Python iterables to one plain value using a caller-provided combining function.\n // example 1: reduce((a, b) => a + b, [1, 2, 3, 4])\n // returns 1: 10\n // example 2: reduce((a, b) => a + b, 'abc')\n // returns 2: 'abc'\n return arguments.length >= 3 ? pythonReduce(func, iterable, initializer) : pythonReduce(func, iterable, noInitializer);\n}" |
| 14 | +const __locutus_standalone_js_code = "// python/_helpers/_functools (Locutus helper dependency)\nconst noInitializer = Symbol('noInitializer');\nfunction pythonReduce(func, iterable, initializer = noInitializer) {\n const iterator = toIterator(iterable, 'reduce');\n let accumulator;\n if (initializer !== noInitializer) {\n accumulator = initializer;\n }\n else {\n const first = iterator.next();\n if (first.done) {\n throw new TypeError('reduce() of empty sequence with no initial value');\n }\n accumulator = first.value;\n }\n while (true) {\n const next = iterator.next();\n if (next.done) {\n return accumulator;\n }\n accumulator = func(accumulator, next.value);\n }\n}\nfunction toIterator(iterable, functionName) {\n if (typeof iterable === 'string') {\n return iterable[Symbol.iterator]();\n }\n if ((typeof iterable === 'object' || typeof iterable === 'function') && iterable !== null) {\n const iterableValue = iterable;\n const iteratorFactory = iterableValue[Symbol.iterator];\n if (typeof iteratorFactory === 'function') {\n return iteratorFactory.call(iterableValue);\n }\n }\n throw new TypeError(`${functionName}() expected an iterable`);\n}\n// python/functools/reduce (target function module)\nfunction reduce(func, iterable, initializer) {\n // discuss at: https://locutus.io/python/functools/reduce/\n // parity verified: Python 3.12\n // original by: Kevin van Zonneveld (https://kvz.io)\n // note 1: Reduces Python iterables to one plain value using a caller-provided combining function.\n // example 1: reduce((a, b) => a + b, [1, 2, 3, 4])\n // returns 1: 10\n // example 2: reduce((a, b) => a + b, 'abc')\n // returns 2: 'abc'\n return arguments.length >= 3 ? pythonReduce(func, iterable, initializer) : pythonReduce(func, iterable, noInitializer);\n}" |
| 15 | + |
| 16 | +const __locutus_eval_function = (compiledCode: string): ((...args: unknown[]) => unknown) => { |
| 17 | + const evaluator = new Function('require', compiledCode + '\nreturn ' + __locutus_func_name + ';') |
| 18 | + return evaluator(__locutus_source_require) as (...args: unknown[]) => unknown |
| 19 | +} |
| 20 | +const __locutus_eval_module_export = (compiledCode: string, exportName: string): ((...args: unknown[]) => unknown) => { |
| 21 | + const module = { exports: {} as { [key: string]: unknown } } |
| 22 | + const exports = module.exports |
| 23 | + const evaluator = new Function('exports', 'module', 'require', compiledCode) |
| 24 | + evaluator(exports, module, __locutus_source_require) |
| 25 | + return module.exports[exportName] as (...args: unknown[]) => unknown |
| 26 | +} |
| 27 | +const __locutus_module_js_fn = __locutus_eval_module_export(__locutus_module_js_code, __locutus_func_name) |
| 28 | +const __locutus_standalone_ts_fn = __locutus_eval_function(__locutus_standalone_ts_code) |
| 29 | +const __locutus_standalone_js_fn = __locutus_eval_function(__locutus_standalone_js_code) |
| 30 | + |
| 31 | +describe('src/python/functools/reduce.ts (tested in test/generated/python/functools/reduce.vitest.ts)', function () { |
| 32 | + it('should pass example 1', function () { |
| 33 | + const expected = 10 |
| 34 | + const __locutus_variants: Array<{ name: string; fn: (...args: unknown[]) => unknown }> = [ |
| 35 | + { name: "source", fn: __locutus_source_fn }, |
| 36 | + { name: "module-js", fn: __locutus_module_js_fn }, |
| 37 | + { name: "standalone-ts", fn: __locutus_standalone_ts_fn }, |
| 38 | + { name: "standalone-js", fn: __locutus_standalone_js_fn }, |
| 39 | + ] |
| 40 | + const __locutus_run_example = (reduce: typeof __locutus_source_fn) => { |
| 41 | + return reduce((a, b) => a + b, [1, 2, 3, 4]) |
| 42 | + } |
| 43 | + for (const __locutus_variant of __locutus_variants) { |
| 44 | + const result = __locutus_run_example(__locutus_variant.fn as typeof __locutus_source_fn) |
| 45 | + expect(result).toEqual(expected) |
| 46 | + } |
| 47 | + }) |
| 48 | + it('should pass example 2', function () { |
| 49 | + const expected = 'abc' |
| 50 | + const __locutus_variants: Array<{ name: string; fn: (...args: unknown[]) => unknown }> = [ |
| 51 | + { name: "source", fn: __locutus_source_fn }, |
| 52 | + { name: "module-js", fn: __locutus_module_js_fn }, |
| 53 | + { name: "standalone-ts", fn: __locutus_standalone_ts_fn }, |
| 54 | + { name: "standalone-js", fn: __locutus_standalone_js_fn }, |
| 55 | + ] |
| 56 | + const __locutus_run_example = (reduce: typeof __locutus_source_fn) => { |
| 57 | + return reduce((a, b) => a + b, 'abc') |
| 58 | + } |
| 59 | + for (const __locutus_variant of __locutus_variants) { |
| 60 | + const result = __locutus_run_example(__locutus_variant.fn as typeof __locutus_source_fn) |
| 61 | + expect(result).toEqual(expected) |
| 62 | + } |
| 63 | + }) |
| 64 | +}) |
0 commit comments