Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Corrections to dict write and pop #2261

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

Merged
merged 2 commits into from
Aug 11, 2023
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
65 changes: 65 additions & 0 deletions integration_tests/test_dict_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from lpython import i32

def test_dict():
d_i32: dict[i32, i32] = {5: 1, 5: 2}
d_str: dict[str, i32] = {'a': 1, 'a': 2}
l_str_1: list[str] = []
l_str_2: list[str] = []
l_i32_1: list[i32] = []
l_i32_2: list[i32] = []
i: i32
s: str

assert len(d_i32) == 1
d_i32.pop(5)
assert len(d_i32) == 0

assert len(d_str) == 1
d_str.pop('a')
assert len(d_str) == 0

d_str = {'a': 2, 'a': 2, 'b': 2, 'c': 3, 'a': 5}
assert len(d_str) == 3
d_str.pop('a')
assert len(d_str) == 2
d_str.pop('b')
assert len(d_str) == 1

d_str['a'] = 20
assert len(d_str) == 2
d_str.pop('c')
assert len(d_str) == 1

l_str_1 = d_str.keys()
for s in l_str_1:
l_str_2.append(s)
assert l_str_2 == ['a']
l_i32_1 = d_str.values()
for i in l_i32_1:
l_i32_2.append(i)
assert l_i32_2 == [20]

d_i32 = {5: 2, 5: 2, 6: 2, 7: 3, 5: 5}
assert len(d_i32) == 3
d_i32.pop(5)
assert len(d_i32) == 2
d_i32.pop(6)
assert len(d_i32) == 1

d_i32[6] = 30
assert len(d_i32) == 2
d_i32.pop(7)
assert len(d_i32) == 1

l_i32_1 = d_i32.keys()
l_i32_2.clear()
for i in l_i32_1:
l_i32_2.append(i)
assert l_i32_2 == [6]
l_i32_1 = d_i32.values()
l_i32_2.clear()
for i in l_i32_1:
l_i32_2.append(i)
assert l_i32_2 == [30]

test_dict()
328 changes: 224 additions & 104 deletions src/libasr/codegen/llvm_utils.cpp

Large diffs are not rendered by default.

12 changes: 1 addition & 11 deletions src/libasr/codegen/llvm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ namespace LCompilers {
void write_item(llvm::Value* dict, llvm::Value* key,
llvm::Value* value, llvm::Module* module,
ASR::ttype_t* key_asr_type, ASR::ttype_t* value_asr_type,
std::map<std::string, std::map<std::string, int>>& name2memidx) = 0;
std::map<std::string, std::map<std::string, int>>& name2memidx);

virtual
llvm::Value* read_item(llvm::Value* dict, llvm::Value* key,
Expand Down Expand Up @@ -693,11 +693,6 @@ namespace LCompilers {
ASR::ttype_t* value_asr_type,
std::map<std::string, std::map<std::string, int>>& name2memidx);

void write_item(llvm::Value* dict, llvm::Value* key,
llvm::Value* value, llvm::Module* module,
ASR::ttype_t* key_asr_type, ASR::ttype_t* value_asr_type,
std::map<std::string, std::map<std::string, int>>& name2memidx);

llvm::Value* read_item(llvm::Value* dict, llvm::Value* key,
llvm::Module& module, ASR::Dict_t* key_asr_type, bool enable_bounds_checking,
bool get_pointer=false);
Expand Down Expand Up @@ -847,11 +842,6 @@ namespace LCompilers {
ASR::ttype_t* value_asr_type,
std::map<std::string, std::map<std::string, int>>& name2memidx);

void write_item(llvm::Value* dict, llvm::Value* key,
llvm::Value* value, llvm::Module* module,
ASR::ttype_t* key_asr_type, ASR::ttype_t* value_asr_type,
std::map<std::string, std::map<std::string, int>>& name2memidx);

llvm::Value* read_item(llvm::Value* dict, llvm::Value* key,
llvm::Module& module, ASR::Dict_t* dict_type, bool enable_bounds_checking,
bool get_pointer=false);
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/test_dict15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lpython import i32

def test_dict_pop():
d: dict[i32, i32] = {1: 2}
d.pop(1)
d.pop(1)

test_dict_pop()
8 changes: 8 additions & 0 deletions tests/errors/test_dict16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lpython import i32

def test_dict_pop():
d: dict[str, i32] = {'a': 2}
d.pop('a')
d.pop('a')

test_dict_pop()
13 changes: 13 additions & 0 deletions tests/reference/runtime-test_dict15-6f3af0d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "runtime-test_dict15-6f3af0d",
"cmd": "lpython {infile}",
"infile": "tests/errors/test_dict15.py",
"infile_hash": "6a0e507b9a9cf659cb433abbdc3435b4c63a6079eadcd7d2c765def1",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "runtime-test_dict15-6f3af0d.stderr",
"stderr_hash": "cb46ef04db0862506d688ebe8830a50afaaead9b0d29b0c007dd149a",
"returncode": 1
}
1 change: 1 addition & 0 deletions tests/reference/runtime-test_dict15-6f3af0d.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KeyError: The dict does not contain the specified key
13 changes: 13 additions & 0 deletions tests/reference/runtime-test_dict16-c5a958d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "runtime-test_dict16-c5a958d",
"cmd": "lpython {infile}",
"infile": "tests/errors/test_dict16.py",
"infile_hash": "7b00cfd7f6eac8338897bd99e5d953605f16927ee0f27683146b0182",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "runtime-test_dict16-c5a958d.stderr",
"stderr_hash": "cb46ef04db0862506d688ebe8830a50afaaead9b0d29b0c007dd149a",
"returncode": 1
}
1 change: 1 addition & 0 deletions tests/reference/runtime-test_dict16-c5a958d.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KeyError: The dict does not contain the specified key