-
Notifications
You must be signed in to change notification settings - Fork 27
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 #178 from tonybaloney/lazy_dictionary
Make the returned dictionary and list types lazy
- Loading branch information
Showing
27 changed files
with
513 additions
and
203 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using CSnakes.Runtime.Python; | ||
|
||
namespace CSnakes.Runtime.Tests.Python; | ||
public class PyDictionaryTests : RuntimeTestBase | ||
{ | ||
[Fact] | ||
public void TestIndex() | ||
{ | ||
var testDict = new Dictionary<string, string>() | ||
{ | ||
["Hello"] = "World?", | ||
["Foo"] = "Bar" | ||
}; | ||
var pyObject = PyObject.From(testDict); | ||
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>(); | ||
Assert.NotNull(pyDict); | ||
Assert.Equal("World?", pyDict["Hello"]); | ||
Assert.Equal("Bar", pyDict["Foo"]); | ||
// Try twice | ||
Assert.Equal("World?", pyDict["Hello"]); | ||
} | ||
|
||
[Fact] | ||
public void TestKeys() | ||
{ | ||
var testDict = new Dictionary<string, string>() | ||
{ | ||
["Hello"] = "World?", | ||
["Foo"] = "Bar" | ||
}; | ||
var pyObject = PyObject.From(testDict); | ||
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>(); | ||
Assert.NotNull(pyDict); | ||
Assert.Equal(2, pyDict.Count); | ||
Assert.Contains("Hello", pyDict.Keys); | ||
Assert.Contains("Foo", pyDict.Keys); | ||
} | ||
|
||
[Fact] | ||
public void TestValues() | ||
{ | ||
var testDict = new Dictionary<string, string>() | ||
{ | ||
["Hello"] = "World?", | ||
["Foo"] = "Bar" | ||
}; | ||
var pyObject = PyObject.From(testDict); | ||
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>(); | ||
Assert.NotNull(pyDict); | ||
Assert.Equal(2, pyDict.Count); | ||
Assert.Contains("World?", pyDict.Values); | ||
Assert.Contains("Bar", pyDict.Values); | ||
} | ||
|
||
[Fact] | ||
public void TestCount() | ||
{ | ||
var testDict = new Dictionary<string, string>() | ||
{ | ||
["Hello"] = "World?", | ||
["Foo"] = "Bar" | ||
}; | ||
var pyObject = PyObject.From(testDict); | ||
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>(); | ||
Assert.NotNull(pyDict); | ||
Assert.Equal(2, pyDict.Count); | ||
} | ||
|
||
[Fact] | ||
public void TestContainsKey() | ||
{ | ||
var testDict = new Dictionary<string, string>() | ||
{ | ||
["Hello"] = "World?", | ||
["Foo"] = "Bar" | ||
}; | ||
var pyObject = PyObject.From(testDict); | ||
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>(); | ||
Assert.NotNull(pyDict); | ||
Assert.True(pyDict.ContainsKey("Hello")); | ||
Assert.True(pyDict.ContainsKey("Foo")); | ||
Assert.False(pyDict.ContainsKey("Bar")); | ||
} | ||
|
||
[Fact] | ||
public void TestTryGetValue() | ||
{ | ||
var testDict = new Dictionary<string, string>() | ||
{ | ||
["Hello"] = "World?", | ||
["Foo"] = "Bar" | ||
}; | ||
var pyObject = PyObject.From(testDict); | ||
var pyDict = pyObject.As<IReadOnlyDictionary<string, string>>(); | ||
Assert.NotNull(pyDict); | ||
Assert.True(pyDict.TryGetValue("Hello", out var value)); | ||
Assert.Equal("World?", value); | ||
Assert.True(pyDict.TryGetValue("Foo", out value)); | ||
Assert.Equal("Bar", value); | ||
Assert.False(pyDict.TryGetValue("Bar", out _)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using CSnakes.Runtime.Python; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace CSnakes.Runtime.CPython; | ||
internal unsafe partial class CPythonAPI | ||
{ | ||
/// <summary> | ||
/// Return the next value from the iterator o. | ||
/// The object must be an iterator according to PyIter_Check() | ||
/// (it is up to the caller to check this). | ||
/// If there are no remaining values, returns NULL with no exception set. | ||
/// If an error occurs while retrieving the item, returns NULL and passes along the exception. | ||
/// </summary> | ||
/// <param name="iter"></param> | ||
/// <returns>New refernce to the next item</returns> | ||
[LibraryImport(PythonLibraryName)] | ||
internal static partial nint PyIter_Next(PyObject iter); | ||
} |
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
Oops, something went wrong.