Enable more examples in the documentation for testing

This commit is contained in:
Alex Beregszaszi 2017-07-17 11:24:18 +02:00
parent 01fbc63623
commit cffe52d6d9
6 changed files with 46 additions and 28 deletions

View File

@ -316,6 +316,8 @@ For example,
:: ::
pragma solidity ^0.4.0;
contract Test { contract Test {
function Test(){ b = 0x12345678901234567890123456789012; } function Test(){ b = 0x12345678901234567890123456789012; }
event Event(uint indexed a, bytes32 b) event Event(uint indexed a, bytes32 b)

View File

@ -490,7 +490,7 @@ is performed by replacing the variable's value on the stack by the new value.
.. code:: .. code::
assembly { {
let v := 0 // functional-style assignment as part of variable declaration let v := 0 // functional-style assignment as part of variable declaration
let g := add(v, 2) let g := add(v, 2)
sload(10) sload(10)
@ -509,7 +509,7 @@ case called ``default``.
.. code:: .. code::
assembly { {
let x := 0 let x := 0
switch calldataload(4) switch calldataload(4)
case 0 { case 0 {
@ -538,7 +538,7 @@ The following example computes the sum of an area in memory.
.. code:: .. code::
assembly { {
let x := 0 let x := 0
for { let i := 0 } lt(i, 0x100) { i := add(i, 0x20) } { for { let i := 0 } lt(i, 0x100) { i := add(i, 0x20) } {
x := add(x, mload(i)) x := add(x, mload(i))
@ -565,7 +565,7 @@ The following example implements the power function by square-and-multiply.
.. code:: .. code::
assembly { {
function power(base, exponent) -> result { function power(base, exponent) -> result {
switch exponent switch exponent
case 0 { result := 1 } case 0 { result := 1 }

View File

@ -968,6 +968,8 @@ Interfaces are denoted by their own keyword:
:: ::
pragma solidity ^0.4.11;
interface Token { interface Token {
function transfer(address recipient, uint amount); function transfer(address recipient, uint amount);
} }

View File

@ -128,7 +128,6 @@ the gas can be specified with special options ``.value()`` and ``.gas()``, respe
function info() payable returns (uint ret) { return 42; } function info() payable returns (uint ret) { return 42; }
} }
contract Consumer { contract Consumer {
InfoFeed feed; InfoFeed feed;
function setFeed(address addr) { feed = InfoFeed(addr); } function setFeed(address addr) { feed = InfoFeed(addr); }
@ -231,7 +230,6 @@ creation-dependencies are not possible.
} }
} }
contract C { contract C {
D d = new D(4); // will be executed as part of C's constructor D d = new D(4); // will be executed as part of C's constructor

View File

@ -116,6 +116,8 @@ array in the return statement. Pretty cool, huh?
Example:: Example::
pragma solidity ^0.4.0;
contract C { contract C {
function f() returns (uint8[5]) { function f() returns (uint8[5]) {
string[4] memory adaArr = ["This", "is", "an", "array"]; string[4] memory adaArr = ["This", "is", "an", "array"];
@ -192,6 +194,8 @@ should be noted that you must declare them as static memory arrays.
Examples:: Examples::
pragma solidity ^0.4.0;
contract C { contract C {
struct S { struct S {
uint a; uint a;
@ -200,10 +204,9 @@ Examples::
S public x = S(1, 2); S public x = S(1, 2);
string name = "Ada"; string name = "Ada";
string[4] memory adaArr = ["This", "is", "an", "array"]; string[4] adaArr = ["This", "is", "an", "array"];
} }
contract D { contract D {
C c = new C(); C c = new C();
} }
@ -243,6 +246,8 @@ which will be extended in the future. In addition, Arachnid has written `solidit
For now, if you want to modify a string (even when you only want to know its length), For now, if you want to modify a string (even when you only want to know its length),
you should always convert it to a ``bytes`` first:: you should always convert it to a ``bytes`` first::
pragma solidity ^0.4.0;
contract C { contract C {
string s; string s;
@ -288,6 +293,8 @@ situation.
If you do not want to throw, you can return a pair:: If you do not want to throw, you can return a pair::
pragma solidity ^0.4.0;
contract C { contract C {
uint[] counters; uint[] counters;
@ -302,9 +309,9 @@ If you do not want to throw, you can return a pair::
function checkCounter(uint index) { function checkCounter(uint index) {
var (counter, error) = getCounter(index); var (counter, error) = getCounter(index);
if (error) { if (error) {
... // ...
} else { } else {
... // ...
} }
} }
} }
@ -363,6 +370,8 @@ of variable it concerns:
Example:: Example::
pragma solidity ^0.4.0;
contract C { contract C {
uint[] data1; uint[] data1;
uint[] data2; uint[] data2;
@ -375,7 +384,7 @@ Example::
append(data2); append(data2);
} }
function append(uint[] storage d) { function append(uint[] storage d) internal {
d.push(1); d.push(1);
} }
} }
@ -393,6 +402,9 @@ A common mistake is to declare a local variable and assume that it will
be created in memory, although it will be created in storage:: be created in memory, although it will be created in storage::
/// THIS CONTRACT CONTAINS AN ERROR /// THIS CONTRACT CONTAINS AN ERROR
pragma solidity ^0.4.0;
contract C { contract C {
uint someVariable; uint someVariable;
uint[] data; uint[] data;
@ -417,6 +429,8 @@ slot ``0``) is modified by ``x.push(2)``.
The correct way to do this is the following:: The correct way to do this is the following::
pragma solidity ^0.4.0;
contract C { contract C {
uint someVariable; uint someVariable;
uint[] data; uint[] data;
@ -533,11 +547,12 @@ In the case of a ``contract A`` calling a new instance of ``contract B``, parent
You will need to make sure that you have both contracts aware of each other's presence and that ``contract B`` has a ``payable`` constructor. You will need to make sure that you have both contracts aware of each other's presence and that ``contract B`` has a ``payable`` constructor.
In this example:: In this example::
pragma solidity ^0.4.0;
contract B { contract B {
function B() payable {} function B() payable {}
} }
contract A { contract A {
address child; address child;
@ -580,6 +595,8 @@ Can a contract pass an array (static size) or string or ``bytes`` (dynamic size)
Sure. Take care that if you cross the memory / storage boundary, Sure. Take care that if you cross the memory / storage boundary,
independent copies will be created:: independent copies will be created::
pragma solidity ^0.4.0;
contract C { contract C {
uint[20] x; uint[20] x;
@ -588,11 +605,11 @@ independent copies will be created::
h(x); h(x);
} }
function g(uint[20] y) { function g(uint[20] y) internal {
y[2] = 3; y[2] = 3;
} }
function h(uint[20] storage y) { function h(uint[20] storage y) internal {
y[3] = 4; y[3] = 4;
} }
} }

View File

@ -210,4 +210,3 @@ for the two input parameters and two returned values.
p = 2 * (w + h); p = 2 * (w + h);
} }
} }