-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3350 from quantified-uncertainty/simpleValueSummary
Added summary for simpleValue
- Loading branch information
Showing
12 changed files
with
11,831 additions
and
14,281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/squiggle-lang/__tests__/utility/FormattedNumber_test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { | ||
FormatterOptions, | ||
makeFormattedNumber, | ||
numberToFormattedNumberString, | ||
} from "../../src/utility/FormattedNumber.js"; | ||
|
||
describe("FormattedNumber", () => { | ||
const makeTestCases: [number, ReturnType<typeof makeFormattedNumber>][] = [ | ||
[123.45, { type: "basic", value: "120", isNegative: false }], | ||
[-123.45, { type: "basic", value: "120", isNegative: true }], | ||
[1234, { type: "basic", value: "1200", isNegative: false }], | ||
[ | ||
1234567, | ||
{ type: "unit", mantissa: "1.2", symbol: "M", isNegative: false }, | ||
], | ||
[0, { type: "basic", value: "0", isNegative: false }], | ||
[Infinity, { type: "basic", value: "Infinity", isNegative: false }], | ||
[ | ||
0.0000001, | ||
{ type: "scientific", mantissa: "1", exponent: -7, isNegative: false }, | ||
], | ||
[ | ||
1e20, | ||
{ type: "scientific", mantissa: "1", exponent: 20, isNegative: false }, | ||
], | ||
[-Infinity, { type: "basic", value: "Infinity", isNegative: true }], | ||
[NaN, { type: "basic", value: "NaN", isNegative: false }], | ||
]; | ||
|
||
const makeWithOptionsTestCases: [ | ||
number, | ||
FormatterOptions, | ||
ReturnType<typeof makeFormattedNumber>, | ||
][] = [ | ||
[ | ||
123.45, | ||
{ precision: 1 }, | ||
{ type: "basic", value: "100", isNegative: false }, | ||
], | ||
[ | ||
1234567, | ||
{ precision: 3 }, | ||
{ type: "unit", mantissa: "1.23", symbol: "M", isNegative: false }, | ||
], | ||
[ | ||
0.0001, | ||
{ forceScientific: true }, | ||
{ type: "scientific", mantissa: "1", exponent: -4, isNegative: false }, | ||
], | ||
[ | ||
1000, | ||
{ forceScientific: true }, | ||
{ type: "basic", value: "1000", isNegative: false }, | ||
], | ||
[ | ||
1, | ||
{ forceScientific: true }, | ||
{ type: "basic", value: "1", isNegative: false }, | ||
], | ||
[ | ||
9876, | ||
{ precision: 4 }, | ||
{ type: "basic", value: "9876", isNegative: false }, | ||
], | ||
]; | ||
|
||
const makeAndToStringTestCases = [ | ||
[1, "1"], | ||
[123.45, "120"], | ||
[-123.45, "-120"], | ||
[1234, "1200"], | ||
[1234567, "1.2M"], | ||
[0, "0"], | ||
[Infinity, "Infinity"], | ||
[0.0000001, "1e-7"], | ||
[1e20, "1e20"], | ||
[-Infinity, "-Infinity"], | ||
[NaN, "NaN"], | ||
]; | ||
|
||
describe("make", () => { | ||
test.each(makeTestCases)("make(%p) -> %p", (input, expected) => { | ||
expect(makeFormattedNumber(input)).toEqual(expected); | ||
}); | ||
|
||
test.each(makeWithOptionsTestCases)( | ||
"make(%p, %p) -> %p", | ||
(input, options, expected) => { | ||
expect(makeFormattedNumber(input, options)).toEqual(expected); | ||
} | ||
); | ||
}); | ||
|
||
describe("makeAndToString", () => { | ||
test.each(makeAndToStringTestCases)( | ||
"makeAndToString(%p) -> %p", | ||
(input, expected) => { | ||
expect(numberToFormattedNumberString(input as number)).toBe(expected); | ||
} | ||
); | ||
}); | ||
}); |
106 changes: 106 additions & 0 deletions
106
packages/squiggle-lang/__tests__/utility/compactTreeString_test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { compactTreeString } from "../../src/utility/compactTreeString.js"; | ||
import { | ||
removeLambdas, | ||
SimpleValue, | ||
simpleValueFromAny, | ||
SimpleValueWithoutLambda, | ||
} from "../../src/value/simpleValue.js"; | ||
import { testRun } from "../helpers/helpers.js"; | ||
|
||
async function runAndSummarize(code: string): Promise<string> { | ||
const result = await testRun(code); | ||
const simpleValue: SimpleValue = simpleValueFromAny(result.result.asJS()); | ||
const withoutLambdas: SimpleValueWithoutLambda = removeLambdas(simpleValue); | ||
return compactTreeString(withoutLambdas, { | ||
maxDepth: 4, | ||
maxArrayItems: 4, | ||
maxDictItems: 4, | ||
}); | ||
} | ||
|
||
describe("compactTreeString", () => { | ||
test("Simple array", () => { | ||
const result = compactTreeString([1, 2, 3, 4, 5]); | ||
expect(result).toBe("[1, 2, 3, 4, 5]"); | ||
}); | ||
|
||
test("Nested array", () => { | ||
const result = compactTreeString([1, [2, 3], 4, [5, 6]]); | ||
expect(result).toBe("[1, [2, 3], 4, [5, 6]]"); | ||
}); | ||
|
||
test("Nested object", async () => { | ||
const result = await runAndSummarize(`{a: {b: 1, c: 2}, d: [3, 4]}`); | ||
expect(result).toBe(`{ | ||
vtype: "Dict", | ||
value: { | ||
a: { | ||
vtype: "Dict", | ||
value: { | ||
b: ..., | ||
c: ... | ||
} | ||
}, | ||
d: [3, 4] | ||
} | ||
}`); | ||
}); | ||
|
||
test("Long array", async () => { | ||
const result = await runAndSummarize( | ||
`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]` | ||
); | ||
expect(result).toBe("[1, 2, 3, 4, ...15 total]"); | ||
}); | ||
|
||
test("Long object (hash)", async () => { | ||
const result = await runAndSummarize( | ||
`{a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15}` | ||
); | ||
expect(result).toBe(`{ | ||
vtype: "Dict", | ||
value: { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
d: 4, | ||
...15 total | ||
} | ||
}`); | ||
}); | ||
|
||
test("complex object", async () => { | ||
const result = await runAndSummarize( | ||
`x = {foo: {bar: 1 to 5}} | ||
y = 5000 to 10000 | ||
z(f) = f + 3 | ||
{x,y,z}` | ||
); | ||
expect(result).toBe(`{ | ||
vtype: "Dict", | ||
value: { | ||
x: { | ||
vtype: "Dict", | ||
value: { | ||
foo: ... | ||
} | ||
}, | ||
y: { | ||
vType: "SampleSetDist", | ||
samples: [10400, 6830, 5280, 7700, ...1000 total], | ||
summary: { | ||
mean: ..., | ||
p5: ..., | ||
p50: ..., | ||
p95: ... | ||
} | ||
}, | ||
z: { | ||
vType: "Lambda", | ||
toString: "(f) => internal code", | ||
paramenterString: "f" | ||
} | ||
} | ||
}`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.