-
Notifications
You must be signed in to change notification settings - Fork 121
Alist unit testing #2339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zonespace27
wants to merge
13
commits into
OpenDreamProject:master
Choose a base branch
from
Zonespace27:alist-unit-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Alist unit testing #2339
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f2493d8
Alist unit testing
Zonespace27 4b35f5c
WIP
Zonespace27 125dd14
more work
Zonespace27 db04fd0
Merge branch 'alist-unit-tests' of https://github.com/Zonespace27/Ope…
johndoe2013 676957a
Merge branch 'master' into alist-unit-tests
Zonespace27 415545a
alist stuff done
johndoe2013 0dcb779
fix tests
johndoe2013 10bad05
compounded test
johndoe2013 55af62c
fix some stuff considered "bad"
johndoe2013 50137cd
whoops
johndoe2013 7a758fc
lint fixes
johndoe2013 cb2e90b
fix 2
johndoe2013 a30e1e2
req. changes
johndoe2013 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,9 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
AL.Add("c", "d") | ||
ASSERT(AL["c"] == -4) | ||
ASSERT(AL["d"] == null) | ||
|
||
AL.Add(list("c", "d")) | ||
ASSERT(AL["c"] == -4) | ||
ASSERT(AL["d"] == null) |
This file contains hidden or 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,6 @@ | ||
// RUNTIME ERROR | ||
|
||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
var/alist/AL2 | ||
AL2 = AL.Copy(1, 2) |
7 changes: 7 additions & 0 deletions
7
Content.Tests/DMProject/Tests/List/alist/AListCompoundedLists.dm
This file contains hidden or 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,7 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("left"=alist("one"=1,"two"=2),"right"=1) | ||
ASSERT(istype(AL["left"], /alist)) | ||
ASSERT(AL["left"]["one"] == 1) | ||
ASSERT(AL["left"]["two"] == 2) | ||
ASSERT(AL["right"] == 1) | ||
ASSERT(!("two" in AL)) |
This file contains hidden or 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,8 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
var/alist/AL2 | ||
AL2 = AL.Copy() | ||
ASSERT(AL["a"] == 1) | ||
ASSERT(AL2["a"] == 1) | ||
ASSERT(AL2["b"] == 2) | ||
ASSERT("c" in AL2) |
This file contains hidden or 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,5 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
AL.Cut() | ||
ASSERT(length(AL) == 0) | ||
ASSERT(!("a" in AL)) |
This file contains hidden or 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,5 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
ASSERT(AL.Find("a")) | ||
ASSERT(AL.Find("a", 2, 3)) | ||
ASSERT(!AL.Find("d")) |
This file contains hidden or 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,5 @@ | ||
// RUNTIME ERROR | ||
|
||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = -2, "c" = 5.05) | ||
AL.Insert(2, "d", "e") |
This file contains hidden or 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,6 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
var/string_output = AL.Join(",") | ||
ASSERT(string_output == "a,b,c") | ||
string_output = AL.Join("", 2) | ||
ASSERT(string_output == "bc") |
20 changes: 20 additions & 0 deletions
20
Content.Tests/DMProject/Tests/List/alist/AListProcInteractions.dm
This file contains hidden or 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,20 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
values_cut_over(AL, 0) | ||
ASSERT(AL["a"] == null) | ||
ASSERT(AL["c"] == -4) | ||
ASSERT(length(AL) == 1) | ||
|
||
AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
values_cut_under(AL, 0) | ||
ASSERT(AL["a"] == 1) | ||
ASSERT(AL["c"] == null) | ||
ASSERT(length(AL) == 2) | ||
|
||
AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
var/alist/AL2 = alist("c" = 5, "d" = 6, "e" = 2) | ||
ASSERT(values_dot(AL, AL2) == -20) | ||
|
||
AL = alist("a" = 1, "b" = 2, "c" = -5) | ||
ASSERT(values_product(AL) == -10) | ||
ASSERT(values_sum(AL) == -2) |
This file contains hidden or 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,6 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
ASSERT(AL.Remove("a", "b")) | ||
ASSERT(!("a" in AL)) | ||
ASSERT(!("b" in AL)) | ||
ASSERT("c" in AL) |
This file contains hidden or 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,6 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = 2, "c" = -4) | ||
ASSERT(AL.RemoveAll("a", "b")) | ||
ASSERT(!("a" in AL)) | ||
ASSERT(!("b" in AL)) | ||
ASSERT("c" in AL) |
This file contains hidden or 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,7 @@ | ||
/proc/RunTest() | ||
var/alist/A1 = alist("1" = 1) | ||
ASSERT(A1["1"] == 1) | ||
A1["1"] = 1.5 | ||
A1["2"] = 2 | ||
ASSERT(A1["1"] == 1.5) | ||
ASSERT(A1["2"] == 2) |
This file contains hidden or 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,5 @@ | ||
// RUNTIME ERROR | ||
|
||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = -2, "c" = 5.05) | ||
AL.Splice(1, 1, "d") |
This file contains hidden or 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,5 @@ | ||
// RUNTIME ERROR | ||
|
||
/proc/RunTest() | ||
var/alist/AL = alist("a" = 1, "b" = -2, "c" = 5.05) | ||
AL.Swap(1, 1) |
14 changes: 14 additions & 0 deletions
14
Content.Tests/DMProject/Tests/List/alist/AListValidKeys.dm
This file contains hidden or 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,14 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist() | ||
var/datum/D = new() | ||
var/list/L = new() | ||
AL[D] = 1 | ||
AL["A"] = 2 | ||
AL[5] = 3 | ||
AL[L] = 4 | ||
AL[null] = 5 | ||
ASSERT(AL[D] == 1) | ||
ASSERT(AL["A"] == 2) | ||
ASSERT(AL[5] == 3) | ||
ASSERT(AL[L] == 4) | ||
ASSERT(AL[null] == 5) |
14 changes: 14 additions & 0 deletions
14
Content.Tests/DMProject/Tests/List/alist/AListValidValues.dm
This file contains hidden or 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,14 @@ | ||
/proc/RunTest() | ||
var/alist/AL = alist() | ||
var/datum/D = new() | ||
var/list/L = new() | ||
AL[1] = D | ||
AL[2] = L | ||
AL[3] = 3 | ||
AL[4] = "4" | ||
AL[5] = null | ||
ASSERT(AL[1] == D) | ||
ASSERT(AL[2] == L) | ||
ASSERT(AL[3] == 3) | ||
ASSERT(AL[4] == "4") | ||
ASSERT(AL[5] == null) |
This file contains hidden or 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 hidden or 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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,18 +1,26 @@ | ||||||||||||||||||||
using System.Linq; | ||||||||||||||||||||
using OpenDreamRuntime.Procs; | ||||||||||||||||||||
|
||||||||||||||||||||
namespace OpenDreamRuntime.Objects.Types; | ||||||||||||||||||||
|
||||||||||||||||||||
// TODO: An arglist given to New() can be used to initialize an alist with values | ||||||||||||||||||||
public sealed class DreamAssocList(DreamObjectDefinition aListDef, int size) : DreamObject(aListDef), IDreamList { | ||||||||||||||||||||
public bool IsAssociative => true; | ||||||||||||||||||||
|
||||||||||||||||||||
private readonly Dictionary<DreamValue, DreamValue> _values = new(size); | ||||||||||||||||||||
|
||||||||||||||||||||
public DreamAssocList(DreamObjectDefinition listDef, Dictionary<DreamValue, DreamValue>? values) : this(listDef, values?.Count ?? 0) { | ||||||||||||||||||||
if (values != null) { | ||||||||||||||||||||
_values = values; | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void SetValue(DreamValue key, DreamValue value, bool allowGrowth = false) { | ||||||||||||||||||||
_values[key] = value; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public DreamValue GetValue(DreamValue key) { | ||||||||||||||||||||
if (!_values.TryGetValue(key, out var value)) | ||||||||||||||||||||
throw new Exception($"No value with the key {key}"); | ||||||||||||||||||||
return DreamValue.Null; | ||||||||||||||||||||
|
||||||||||||||||||||
return value; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
@@ -21,10 +29,42 @@ public bool ContainsKey(DreamValue key) { | |||||||||||||||||||
return _values.ContainsKey(key); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public override DreamValue OperatorIndex(DreamValue index, DMProcState state) { | ||||||||||||||||||||
return GetValue(index); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public IEnumerable<DreamValue> EnumerateValues() { | ||||||||||||||||||||
return _values.Keys; // The keys, counter-intuitively | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public int GetLength() { | ||||||||||||||||||||
return _values.Count; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void Cut(int start = 1, int end = 0) { | ||||||||||||||||||||
if (start != 1) { | ||||||||||||||||||||
throw new Exception($"Cut() was called with non-default start value of {start}."); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (end != 0) { | ||||||||||||||||||||
|
||||||||||||||||||||
throw new Exception($"Cut() was called with non-default end value of {end}."); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
_values.Clear(); | ||||||||||||||||||||
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
public List<DreamValue> GetValues() { | ||||||||||||||||||||
return _values.Keys.ToList(); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public Dictionary<DreamValue, DreamValue> GetAssociativeValues() { | ||||||||||||||||||||
return _values; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void RemoveValue(DreamValue value) { | ||||||||||||||||||||
_values.Remove(value); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public IEnumerable<KeyValuePair<DreamValue, DreamValue>> EnumerateAssocValues() { | ||||||||||||||||||||
return _values; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
@@ -39,4 +79,55 @@ public DreamValue[] CopyToArray() { | |||||||||||||||||||
public Dictionary<DreamValue, DreamValue> CopyAssocValues() { | ||||||||||||||||||||
return new(_values); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void AddValue(DreamValue value) { | ||||||||||||||||||||
if(ContainsValue(value)) { | ||||||||||||||||||||
return; // calling Add("c") on alist("c" = 5) does not change anything | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
_values[value] = DreamValue.Null; | ||||||||||||||||||||
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public IDreamList CreateCopy(int start = 1, int end = 0) { | ||||||||||||||||||||
if (start == 0) ++start; //start being 0 and start being 1 are equivalent | ||||||||||||||||||||
|
||||||||||||||||||||
var values = GetValues(); | ||||||||||||||||||||
if (end > values.Count + 1 || start > values.Count + 1) throw new Exception("list index out of bounds"); | ||||||||||||||||||||
if (end == 0) end = values.Count + 1; | ||||||||||||||||||||
if (end <= start) | ||||||||||||||||||||
return new DreamAssocList(ObjectDefinition, 0); | ||||||||||||||||||||
|
||||||||||||||||||||
Dictionary<DreamValue, DreamValue> copyValues = new(_values); | ||||||||||||||||||||
|
||||||||||||||||||||
return new DreamAssocList(ObjectDefinition, copyValues); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public int FindValue(DreamValue value, int start = 1, int end = 0) { | ||||||||||||||||||||
// Unlike list.Find(), alist.Find() doesn't pay attention to start and end, and returns a boolean 0/1 instead of the position of the found object | ||||||||||||||||||||
if(ContainsValue(value)) { | ||||||||||||||||||||
return 1; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return 0; | ||||||||||||||||||||
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void Insert(int index, DreamValue value) { | ||||||||||||||||||||
throw new Exception("insert not allowed for this list"); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public void Swap(int index1, int index2) { | ||||||||||||||||||||
throw new Exception("swap not allowed for this list"); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
public bool ContainsValue(DreamValue value) { | ||||||||||||||||||||
var keys = GetValues(); | ||||||||||||||||||||
foreach (var key in keys) { | ||||||||||||||||||||
if (key.Equals(value)) { | ||||||||||||||||||||
return true; | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
return false; | ||||||||||||||||||||
Comment on lines
+123
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dictionary can do a much faster hash check
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
} |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.