Skip to content

Commit 0b0db25

Browse files
committed
Step 14 -- Fixed Size Array
1 parent 0f26d1c commit 0b0db25

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-0
lines changed

14_fixed_size_arrays/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fixed Size Array
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pragma solidity ^0.4.23;
2+
contract Contract1 {
3+
4+
int[3] a = [3,4,5];
5+
int[3] b;
6+
function demoFixedSizeStateArray() public returns (int) {
7+
a[1] = 5;
8+
b = [21,22,23];
9+
uint[3] memory bb = [uint(23),34,4];
10+
int[3] memory c = [int(31),32,33]; // explicitly mentioned as int which is int256
11+
c[2] = 56;
12+
return a[1];
13+
}
14+
15+
16+
// Return type and function parameter must have to specify size
17+
// of array to accept in parameter and return from function
18+
function demoFixedSizeArrayReturns() public returns (int[3]) {
19+
int[3] memory c = [int(31),32,33];
20+
return c;
21+
}
22+
23+
24+
int[3] c;
25+
function demoFixedSizeStateArray2() public returns (int) {
26+
// completely different from other languages,
27+
// here it initialized all elments of array with default 0
28+
// we have not initialized the array 'c'
29+
return c[1]; // returns 0
30+
}
31+
32+
function demoFixedSizeStateArray3() public returns (int) {
33+
c[2] = 45;
34+
return c[2];
35+
}
36+
37+
function demoFixedSizeMemoryArray4() public returns (int) {
38+
// completely different from other languages,
39+
// here it initialized all elments of array with default 0
40+
int[3] memory d; // = [int(2),3,4];
41+
return d[1];
42+
}
43+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 testFixedSizeArray() public {
9+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
10+
int a = meta.demoFixedSizeStateArray();
11+
12+
Assert.equal(a,5,"A should be 5");
13+
14+
}
15+
16+
function testFixedSizeArray2() public {
17+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
18+
// here we need to specify memory keyword otherwise it will not work
19+
int[3] memory a = meta.demoFixedSizeArrayReturns();
20+
Assert.equal(a[1],32,"A should be 32");
21+
}
22+
23+
function testFixedSizeArray3() public {
24+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
25+
int a = meta.demoFixedSizeStateArray2();
26+
Assert.equal(a,0,"A should be 0");
27+
28+
}
29+
30+
function testFixedSizeArray4() public {
31+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
32+
int a = meta.demoFixedSizeStateArray3();
33+
Assert.equal(a,45,"A should be 45");
34+
35+
}
36+
37+
function testFixedSizeArray5() public {
38+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
39+
int a = meta.demoFixedSizeMemoryArray4();
40+
Assert.equal(a,0,"A should be 0");
41+
}
42+
}
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+
};

14_fixed_size_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)