Skip to content

Commit

Permalink
tests for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
plotchy committed Jul 22, 2024
1 parent 88d6a95 commit 801de39
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/pyrometer/tests/no_killed_ctxs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ fn test_condop() {
assert_no_parse_errors(path_str);
}


#[test]
fn test_delete() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let path_str = format!("{manifest_dir}/tests/test_data/delete.sol");
let sol = include_str!("./test_data/delete.sol");
assert_no_ctx_killed(path_str, sol);
}

#[test]
fn test_dyn_types() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
Expand Down
138 changes: 138 additions & 0 deletions crates/pyrometer/tests/test_data/delete.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
pragma solidity ^0.8.0;

contract ComplexDelete {
struct ContactInfo {
string email;
string phone;
}

struct Address {
string street;
string city;
string country;
uint256 postalCode;
}

struct Employment {
string company;
string position;
uint256 startDate;
uint256 endDate;
}

struct Education {
string institution;
string degree;
uint256 graduationYear;
}

struct User {
uint256 id;
string name;
ContactInfo contactInfo;
Address[] addresses;
Employment[] employmentHistory;
Education[] educationHistory;
mapping(string => bool) preferences;
}

mapping(uint256 => User) public users;
uint256[] public userIds;

function addUser(
uint256 id,
string memory name,
string memory email,
string memory phone
) public {
require(users[id].id == 0, "User already exists");
User storage newUser = users[id];
newUser.id = id;
newUser.name = name;
newUser.contactInfo = ContactInfo(email, phone);
userIds.push(id);
}

function addUserAddress(
uint256 userId,
string memory street,
string memory city,
string memory country,
uint256 postalCode
) public {
users[userId].addresses.push(Address(street, city, country, postalCode));
}

function addEmploymentHistory(
uint256 userId,
string memory company,
string memory position,
uint256 startDate,
uint256 endDate
) public {
users[userId].employmentHistory.push(Employment(company, position, startDate, endDate));
}

function addEducationHistory(
uint256 userId,
string memory institution,
string memory degree,
uint256 graduationYear
) public {
users[userId].educationHistory.push(Education(institution, degree, graduationYear));
}

function setUserPreference(uint256 userId, string memory key, bool value) public {
users[userId].preferences[key] = value;
}

function deleteUser(uint256 userId) public {
require(users[userId].id != 0, "User does not exist");
delete users[userId];
for (uint256 i = 0; i < userIds.length; i++) {
if (userIds[i] == userId) {
userIds[i] = userIds[userIds.length - 1];
userIds.pop();
break;
}
}
}

function deleteUserAddress(uint256 userId, uint256 addressIndex) public {
require(addressIndex < users[userId].addresses.length, "Address index out of bounds");
users[userId].addresses[addressIndex] = users[userId].addresses[users[userId].addresses.length - 1];
users[userId].addresses.pop();
}

function deleteEmploymentHistory(uint256 userId, uint256 employmentIndex) public {
require(employmentIndex < users[userId].employmentHistory.length, "Employment index out of bounds");
users[userId].employmentHistory[employmentIndex] = users[userId].employmentHistory[users[userId].employmentHistory.length - 1];
users[userId].employmentHistory.pop();
}

function deleteEducationHistory(uint256 userId, uint256 educationIndex) public {
require(educationIndex < users[userId].educationHistory.length, "Education index out of bounds");
users[userId].educationHistory[educationIndex] = users[userId].educationHistory[users[userId].educationHistory.length - 1];
users[userId].educationHistory.pop();
}

function deleteUserPreference(uint256 userId, string memory key) public {
delete users[userId].preferences[key];
}

function updateContactInfo(uint256 userId, string memory newEmail, string memory newPhone) public {
users[userId].contactInfo = ContactInfo(newEmail, newPhone);
}

function clearAllUserAddresses(uint256 userId) public {
delete users[userId].addresses;
}

function clearAllEmploymentHistory(uint256 userId) public {
delete users[userId].employmentHistory;
}

function clearAllEducationHistory(uint256 userId) public {
delete users[userId].educationHistory;
}
}

0 comments on commit 801de39

Please sign in to comment.