Skip to content

Commit 5376bff

Browse files
committed
Step 15 - Dynamic Arrays
1 parent 0b0db25 commit 5376bff

File tree

8 files changed

+156
-0
lines changed

8 files changed

+156
-0
lines changed

15_dynamic_arrays/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dynamic Arrays
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
pragma solidity ^0.4.23;
2+
contract Contract1 {
3+
4+
int[] a = [3,4,5];
5+
int[] b;
6+
function demoDynamicArray() public returns (int) {
7+
a[1] = 5;
8+
b = [21,22,23];
9+
10+
// Not allowed to create dynamic memory array in this way
11+
//int[] memory c = [int(31),32,33];
12+
//c[2] = 56;
13+
return a[1];
14+
}
15+
16+
function demoDynamicArray2() public returns (int) {
17+
int[] memory c = new int[](3);
18+
return c[1];
19+
}
20+
21+
function demoDynamicArray3() public returns (int[]) {
22+
int[] memory c = new int[](3);
23+
c[1] = 31;
24+
return c;
25+
}
26+
27+
int[] d = [int(2),3,4];
28+
function demoDynamicArray4() public returns (int) {
29+
//int[] memory c = new int[](3);
30+
//int[] memory c = [int(1),2,3];
31+
// push function only works on storage array and that array must be
32+
// initialized already
33+
d.push(34);
34+
return d[3];
35+
}
36+
37+
int[] e = new int[](3);
38+
function demoDynamicArray5() public returns (int) {
39+
40+
// push function only works on storage array and that array must be
41+
// initialized already
42+
// push function will add element at the end of array
43+
// in this case it will be 4th element
44+
d.push(34);
45+
return d[3];
46+
}
47+
48+
49+
50+
int[] f;
51+
function demoDynamicArray6() public returns (int) {
52+
53+
// This will never works because if we have dynamic array
54+
// then it must be initialized first
55+
//f[1] = 45;
56+
57+
58+
return 5; // f[1]; // access will also require initilization first
59+
}
60+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma solidity ^0.4.23;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
8+
constructor() public {
9+
owner = msg.sender;
10+
}
11+
12+
modifier restricted() {
13+
if (msg.sender == owner) _;
14+
}
15+
16+
function setCompleted(uint completed) public restricted {
17+
last_completed_migration = completed;
18+
}
19+
20+
function upgrade(address new_address) public restricted {
21+
Migrations upgraded = Migrations(new_address);
22+
upgraded.setCompleted(last_completed_migration);
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var Migrations = artifacts.require("./Migrations.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(Migrations);
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var contract1 = artifacts.require("./Contract1.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(contract1);
5+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
pragma solidity ^0.4.24;
2+
import "truffle/Assert.sol";
3+
import "truffle/DeployedAddresses.sol";
4+
import "../contracts/Contract1.sol";
5+
6+
contract TestContract1 {
7+
8+
function testDynamicArray() public {
9+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
10+
int a = meta.demoDynamicArray();
11+
12+
Assert.equal(a,5,"A should be 5");
13+
14+
}
15+
16+
function testDynamicArray2() public {
17+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
18+
int a = meta.demoDynamicArray2();
19+
Assert.equal(a,0,"A should be 0");
20+
}
21+
22+
function testDynamicArray3() public {
23+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
24+
int[] memory a = meta.demoDynamicArray3();
25+
Assert.equal(a[1],31,"A should be 31");
26+
}
27+
28+
function testDynamicArray4() public {
29+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
30+
int a = meta.demoDynamicArray4();
31+
Assert.equal(a,34,"A should be 34");
32+
}
33+
34+
function testDynamicArray5() public {
35+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
36+
int a = meta.demoDynamicArray5();
37+
Assert.equal(a,34,"A should be 34");
38+
}
39+
40+
function testDynamicArray6() public {
41+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
42+
int a = meta.demoDynamicArray6();
43+
Assert.equal(a,5,"A should be 45");
44+
}
45+
46+
}

15_dynamic_arrays/truffle-config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
};

15_dynamic_arrays/truffle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
networks: {
5+
development: {
6+
host: "127.0.0.1",
7+
port: 8545,
8+
network_id: "*" // Match any network id
9+
}
10+
}
11+
};

0 commit comments

Comments
 (0)