Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/generics/lib.generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ type MapEntry<TKey, TValue> =
value: TValue; // Value of entry
};

// -mm=own: keys and values are values, strings or Shared<T> handles; an object goes in as Shared<T>
@sharedElements
class Map<K = any, V = any> {

const StartOfFreeList = -3;
Expand Down Expand Up @@ -1091,6 +1093,8 @@ type SetEntry<TValue> =
value: TValue; // Value of entry
};

// -mm=own: elements are values, strings or Shared<T> handles; an object goes in as Shared<T>
@sharedElements
class Set<V = any> {

const StartOfFreeList = -3;
Expand Down
16 changes: 15 additions & 1 deletion tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Runs every test in .\tests, release and debug, compiled and under the JIT.
# .\tests.ps1 the compiler's default memory model (gc)
# .\tests.ps1 -Model rc -mm=rc, against the default library built for it; also $Env:TSLANG_MM
# A test whose first line starts with "// gc only" is skipped under any other model.
# A test whose first line starts with "// gc only" is skipped under any other model, and one whose
# first line starts with "// not own" under -mm=own.
param([string]$Model = $Env:TSLANG_MM)

# The library paths the environment gives when the script starts. One it does not give is worked
Expand Down Expand Up @@ -132,6 +133,13 @@ function Test-GcOnly([string]$path)
return (Get-Content $path -TotalCount 1) -match '^// gc only'
}

# A test of what -mm=own refuses by design says so on its first line ("// not own: ..."), e.g. a
# Map of `any`: under own an object goes into a Map or Set as Shared<T>.
function Test-NotOwn([string]$path)
{
return (Get-Content $path -TotalCount 1) -match '^// not own'
}

function Tests([string]$config, [string]$mode)
{
Write-Host "Testing..."
Expand All @@ -154,6 +162,12 @@ function Tests([string]$config, [string]$mode)
return
}

if ($Model -eq "own" -and (Test-NotOwn $_.FullName)) {
$skipped++
Write-Host "Skipped (not own)" -ForegroundColor Yellow
return
}

$time = (Measure-Command { $result = Test $config $mode $_.Basename }).TotalSeconds
$time = [math]::Round($time, 2).ToString("0.00")

Expand Down
15 changes: 14 additions & 1 deletion tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Runs every test in ./tests, release and debug, compiled and under the JIT.
# ./tests.sh the compiler's default memory model (gc)
# ./tests.sh rc -mm=rc, against the default library built for it; also TSLANG_MM=rc
# A test whose first line starts with "// gc only" is skipped under any other model.
# A test whose first line starts with "// gc only" is skipped under any other model, and one whose
# first line starts with "// not own" under -mm=own.
MODEL="${1:-$TSLANG_MM}"

# The library paths the environment gives when the script starts. One it does not give is worked
Expand Down Expand Up @@ -94,6 +95,12 @@ function gc_only() {
head -n 1 "$1" | grep -q '^// gc only'
}

# A test of what -mm=own refuses by design says so on its first line ("// not own: ..."), e.g. a
# Map of `any`: under own an object goes into a Map or Set as Shared<T>.
function not_own() {
head -n 1 "$1" | grep -q '^// not own'
}

function tests() {
config="$1"
mode="$2"
Expand All @@ -115,6 +122,12 @@ function tests() {
continue
fi

if [ "$MODEL" = "own" ] && not_own "$file"; then
skipped=$((skipped + 1))
printf "\e[33mSkipped\e[0m (not own)\n"
continue
fi

start=$(date +%s.%N)
test_script "$config" "$mode" "$(basename "$file" .ts)"
result=$?
Expand Down
1 change: 1 addition & 0 deletions tests/map_any.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
const m = new Map();

m.set(1, "Hello");
Expand Down
1 change: 1 addition & 0 deletions tests/map_clear.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
const map1 = new Map();

map1.set('bar', 'baz');
Expand Down
1 change: 1 addition & 0 deletions tests/map_entries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
const map1 = new Map();

map1.set('0', 'foo');
Expand Down
1 change: 1 addition & 0 deletions tests/map_forEach.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
new Map([
['foo', 3],
['bar', "test"],
Expand Down
1 change: 1 addition & 0 deletions tests/map_iterator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
const map1 = new Map();

map1.set('0', 'foo');
Expand Down
1 change: 1 addition & 0 deletions tests/map_keys.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
const map1 = new Map();

map1.set('0', 'foo');
Expand Down
59 changes: 59 additions & 0 deletions tests/map_shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Objects in a Map or a Set go through Shared<T>: under -mm=own a plain object or `any` element is
// a compile error. Under rc and own Shared.count counts: the containers give every count back.
class Node {
constructor(public n: number) {}
}

function counted() {
return __MEMORY_MODEL__ === "rc" || __MEMORY_MODEL__ === "own";
}

function fill(x: Shared<Node>, y: Shared<Node>) {
const m = new Map<string, Shared<Node>>();
m.set("a", x);
m.set("b", x);
m.set("a", y);
const got = m.get("b")!;
assert(got.value.n == 7);

let total = 0;
for (const [k, v] of m) total += v.value.n;
assert(total == 15);

total = 0;
for (const v of m.values()) total += v.value.n;
assert(total == 15);

for (const v of m.values()) {
if (v.value.n == 8) break;
}

assert(m.delete("a"));
assert(m.size == 1);
}

function sets(x: Shared<Node>, y: Shared<Node>) {
const a = new Set<Shared<Node>>();
a.add(x);
a.add(y);
const b = new Set<Shared<Node>>();
b.add(x);
const u = a.union(b);
assert(u.size == 2);
assert(a.intersection(b).size == 1);

let total = 0;
for (const v of u) total += v.value.n;
assert(total == 15);
}

const x = new Shared(new Node(7));
const y = new Shared(new Node(8));

fill(x, y);
assert(!counted() || (Shared.count(x) == 1 && Shared.count(y) == 1));

sets(x, y);
assert(!counted() || (Shared.count(x) == 1 && Shared.count(y) == 1));

console.log("ALL DONE");
1 change: 1 addition & 0 deletions tests/map_size.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
const map1 = new Map();

map1.set('a', 'alpha');
Expand Down
1 change: 1 addition & 0 deletions tests/map_values.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// not own: an untyped Map is Map<any, any>, which -mm=own refuses (use typed keys and values)
const map1 = new Map();

map1.set('0', 'foo');
Expand Down