Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.DS_Store
/Carthage/Build
/.build
/Build
/Packages
xcuserdata
/Package.pins
Expand Down
24 changes: 24 additions & 0 deletions BigInt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
903767082801E0F000F4DF61 /* NodeTests+Support.swift in Sources */ = {isa = PBXBuildFile; fileRef = 903767062801E0F000F4DF61 /* NodeTests+Support.swift */; };
903767092801E0F000F4DF61 /* NodeTests+Support.swift in Sources */ = {isa = PBXBuildFile; fileRef = 903767062801E0F000F4DF61 /* NodeTests+Support.swift */; };
9037670A2801E0F000F4DF61 /* NodeTests+Support.swift in Sources */ = {isa = PBXBuildFile; fileRef = 903767062801E0F000F4DF61 /* NodeTests+Support.swift */; };
9037670B2801E0F000F4DF61 /* NodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 903767072801E0F000F4DF61 /* NodeTests.swift */; };
9037670C2801E0F000F4DF61 /* NodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 903767072801E0F000F4DF61 /* NodeTests.swift */; };
9037670D2801E0F000F4DF61 /* NodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 903767072801E0F000F4DF61 /* NodeTests.swift */; };
BB241C441DDB70600067F917 /* Addition.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB241C301DDB70600067F917 /* Addition.swift */; };
BB241C451DDB70600067F917 /* Bitwise Ops.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB241C311DDB70600067F917 /* Bitwise Ops.swift */; };
BB241C461DDB70600067F917 /* Comparable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB241C321DDB70600067F917 /* Comparable.swift */; };
Expand Down Expand Up @@ -149,6 +155,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
903767062801E0F000F4DF61 /* NodeTests+Support.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NodeTests+Support.swift"; sourceTree = "<group>"; };
903767072801E0F000F4DF61 /* NodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NodeTests.swift; sourceTree = "<group>"; };
BB241BC51DD9F6490067F917 /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
BB241BC71DD9F7D60067F917 /* BigInt.podspec */ = {isa = PBXFileReference; explicitFileType = text.script.ruby; path = BigInt.podspec; sourceTree = "<group>"; };
BB241C2F1DDB70600067F917 /* BigInt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BigInt.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -260,6 +268,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
903767052801E0CB00F4DF61 /* Violet - Node */ = {
isa = PBXGroup;
children = (
903767072801E0F000F4DF61 /* NodeTests.swift */,
903767062801E0F000F4DF61 /* NodeTests+Support.swift */,
);
path = "Violet - Node";
sourceTree = "<group>";
};
BB241CCB1DDB71DB0067F917 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -333,6 +350,7 @@
BBB55AC01C8F80020050DDA9 /* Tests */ = {
isa = PBXGroup;
children = (
903767052801E0CB00F4DF61 /* Violet - Node */,
BB241C561DDB70690067F917 /* BigIntTests.swift */,
BB241C571DDB70690067F917 /* BigUIntTests.swift */,
BBEAE4DC1F28C37800F4E329 /* WordTests.swift */,
Expand Down Expand Up @@ -692,6 +710,8 @@
BB4273F61F24AFCC00065766 /* Tools.swift in Sources */,
BBEAE4DD1F28C37800F4E329 /* WordTests.swift in Sources */,
BB241CE41DDB79E30067F917 /* BigUIntTests.swift in Sources */,
903767082801E0F000F4DF61 /* NodeTests+Support.swift in Sources */,
9037670B2801E0F000F4DF61 /* NodeTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -728,6 +748,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
903767092801E0F000F4DF61 /* NodeTests+Support.swift in Sources */,
9037670C2801E0F000F4DF61 /* NodeTests.swift in Sources */,
BB4273F71F24AFCD00065766 /* Tools.swift in Sources */,
BB9916911F33C32000CCFDDC /* WordTests.swift in Sources */,
BB9916901F32610A00CCFDDC /* BigUIntTests.swift in Sources */,
Expand Down Expand Up @@ -802,6 +824,8 @@
BB4273F81F24AFCD00065766 /* Tools.swift in Sources */,
BBEAE4DF1F28C37800F4E329 /* WordTests.swift in Sources */,
BB241CDC1DDB79E20067F917 /* BigUIntTests.swift in Sources */,
9037670A2801E0F000F4DF61 /* NodeTests+Support.swift in Sources */,
9037670D2801E0F000F4DF61 /* NodeTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 5 additions & 0 deletions Scripts/bigint_generate_node_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/dist
/.vscode
package-lock.json
out.swift
42 changes: 42 additions & 0 deletions Scripts/bigint_generate_node_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# What is this?

This script will use [Node.js](https://nodejs.org/en/) to generate tests for our `BigInt`.

Those tests should replace `/Tests/BigIntTests/Violet - Node`.

Example test:

```Swift
func test_add_smi_smi() {
// This whole test has ~500 lines, this is just an extract:
self.addTest(lhs: "0", rhs: "0", expecting: "0")
self.addTest(lhs: "0", rhs: "1", expecting: "1")
self.addTest(lhs: "0", rhs: "-1", expecting: "-1")
self.addTest(lhs: "0", rhs: "2147483647", expecting: "2147483647")
self.addTest(lhs: "1", rhs: "0", expecting: "1")
self.addTest(lhs: "1", rhs: "1", expecting: "2")
self.addTest(lhs: "1", rhs: "-1", expecting: "0")
self.addTest(lhs: "1", rhs: "2147483647", expecting: "2147483648")
self.addTest(lhs: "-1", rhs: "0", expecting: "-1")
self.addTest(lhs: "-1", rhs: "1", expecting: "0")
self.addTest(lhs: "-1", rhs: "-1", expecting: "-2")
self.addTest(lhs: "-1", rhs: "-1073741828", expecting: "-1073741829")
self.addTest(lhs: "2147483647", rhs: "0", expecting: "2147483647")
self.addTest(lhs: "2147483647", rhs: "1", expecting: "2147483648")
self.addTest(lhs: "2147483647", rhs: "-1", expecting: "2147483646")
self.addTest(lhs: "2147483647", rhs: "2147483647", expecting: "4294967294")
}
```

# How to run?

Requires [Node.js](https://nodejs.org/en/).

Run the following commands from the repository root:
```
cd ./Scripts/bigint_generate_node_tests
npm i
./main.sh
```

This will generate `out.swift` file with the result.
2 changes: 2 additions & 0 deletions Scripts/bigint_generate_node_tests/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
./node_modules/.bin/tsc
node ./dist/app.js > out.swift
15 changes: 15 additions & 0 deletions Scripts/bigint_generate_node_tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "generate_big_int_tests",
"version": "1.0.0",
"description": "",
"main": "dst/app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^11.10.2",
"typescript": "^3.9.5"
}
}
16 changes: 16 additions & 0 deletions Scripts/bigint_generate_node_tests/src/all_pairings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface BigIntPair {
lhs: bigint;
rhs: bigint;
}

export function allPossiblePairings(lhsValues: bigint[], rhsValues: bigint[]): BigIntPair[] {
const result: BigIntPair[] = [];

for (const lhs of lhsValues) {
for (const rhs of rhsValues) {
result.push({ lhs, rhs });
}
}

return result;
}
44 changes: 44 additions & 0 deletions Scripts/bigint_generate_node_tests/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
printUnaryOperationTests,
printBinaryOperationTests,
printDivModTests,
printPowerTests,
printShiftOperationTests
} from './printing_functions';

console.log(`\
// So you made a mistake of opening this file...
// Ehh...

import XCTest
@testable import BigInt

/// Tests generated by Node.js.
/// See 'Scripts/bigint_generate_node_tests' for details.
///
/// We could have used some other bigint library (for example GMP),
/// but this is a bit easier to manage.
class NodeTests: XCTestCase {
`);

printUnaryOperationTests('Plus', (a) => a);
printUnaryOperationTests('Minus', (a) => -a);
printUnaryOperationTests('Invert', (a) => ~a);

printBinaryOperationTests('Add', (a, b) => a + b);
printBinaryOperationTests('Sub', (a, b) => a - b);
printBinaryOperationTests('Mul', (a, b) => a * b);
printBinaryOperationTests('Div', (a, b) => a / b);
printBinaryOperationTests('Mod', (a, b) => a % b);

printDivModTests();
printPowerTests();

printBinaryOperationTests('And', (a, b) => a & b);
printBinaryOperationTests('Or', (a, b) => a | b);
printBinaryOperationTests('Xor', (a, b) => a ^ b);

printShiftOperationTests('Left', (a, b) => a << b);
printShiftOperationTests('Right', (a, b) => a >> b);

console.log('}');
71 changes: 71 additions & 0 deletions Scripts/bigint_generate_node_tests/src/number_generators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ===========
// === Smi ===
// ===========

export const smiMax = 2147483647n;
export const smiMin = -2147483648n;

/**
* We will return `2 * countButNotReally + 3` values (don't ask).
*/
export function generateSmiNumbers(countButNotReally: number): bigint[] {
const result: bigint[] = [];

result.push(0n);
result.push(1n);
result.push(-1n);

const count = BigInt(countButNotReally);
const step = smiMax / count;

for (let i = BigInt(0); i < countButNotReally; i++) {
const s = i * step;

const fromMax = smiMax - s;
result.push(fromMax);

const fromMin = smiMin + s;
result.push(fromMin);
}

return result;
}

// ============
// === Heap ===
// ============

export const wordMax = 18446744073709551615n;
export const wordMin = 0n;

/**
* We will return `2 * countButNotReally + 5` values (don't ask).
*/
export function generateHeapNumbers(countButNotReally: number): bigint[] {
const result: bigint[] = [];

result.push(0n);
result.push(1n);
result.push(-1n);
result.push(wordMax);
result.push(-wordMax);

let word = 2n; // Start from '2' and go up
const maxWordCount = 3;

for (let i = 0; i < countButNotReally; i++) {
const min1WordBecauseWeAlreadyAddedZero = 1
const wordCount = (i % maxWordCount) + min1WordBecauseWeAlreadyAddedZero;

let value = 1n;
for (let j = 0; j < wordCount; j++) {
value = value * wordMax + word;
word += 1n;
}

result.push(value);
result.push(-value);
}

return result;
}
Loading