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 {
function Test(){ b = 0x12345678901234567890123456789012; }
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::
assembly {
{
let v := 0 // functional-style assignment as part of variable declaration
let g := add(v, 2)
sload(10)
@ -509,7 +509,7 @@ case called ``default``.
.. code::
assembly {
{
let x := 0
switch calldataload(4)
case 0 {
@ -538,7 +538,7 @@ The following example computes the sum of an area in memory.
.. code::
assembly {
{
let x := 0
for { let i := 0 } lt(i, 0x100) { i := add(i, 0x20) } {
x := add(x, mload(i))
@ -565,7 +565,7 @@ The following example implements the power function by square-and-multiply.
.. code::
assembly {
{
function power(base, exponent) -> result {
switch exponent
case 0 { result := 1 }

View File

@ -968,6 +968,8 @@ Interfaces are denoted by their own keyword:
::
pragma solidity ^0.4.11;
interface Token {
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; }
}
contract Consumer {
InfoFeed feed;
function setFeed(address addr) { feed = InfoFeed(addr); }
@ -231,7 +230,6 @@ creation-dependencies are not possible.
}
}
contract C {
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::
pragma solidity ^0.4.0;
contract C {
function f() returns (uint8[5]) {
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::
pragma solidity ^0.4.0;
contract C {
struct S {
uint a;
@ -200,10 +204,9 @@ Examples::
S public x = S(1, 2);
string name = "Ada";
string[4] memory adaArr = ["This", "is", "an", "array"];
string[4] adaArr = ["This", "is", "an", "array"];
}
contract D {
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),
you should always convert it to a ``bytes`` first::
pragma solidity ^0.4.0;
contract C {
string s;
@ -288,6 +293,8 @@ situation.
If you do not want to throw, you can return a pair::
pragma solidity ^0.4.0;
contract C {
uint[] counters;
@ -302,9 +309,9 @@ If you do not want to throw, you can return a pair::
function checkCounter(uint index) {
var (counter, error) = getCounter(index);
if (error) {
...
// ...
} else {
...
// ...
}
}
}
@ -363,6 +370,8 @@ of variable it concerns:
Example::
pragma solidity ^0.4.0;
contract C {
uint[] data1;
uint[] data2;
@ -375,7 +384,7 @@ Example::
append(data2);
}
function append(uint[] storage d) {
function append(uint[] storage d) internal {
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::
/// THIS CONTRACT CONTAINS AN ERROR
pragma solidity ^0.4.0;
contract C {
uint someVariable;
uint[] data;
@ -417,6 +429,8 @@ slot ``0``) is modified by ``x.push(2)``.
The correct way to do this is the following::
pragma solidity ^0.4.0;
contract C {
uint someVariable;
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.
In this example::
pragma solidity ^0.4.0;
contract B {
function B() payable {}
}
contract A {
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,
independent copies will be created::
pragma solidity ^0.4.0;
contract C {
uint[20] x;
@ -588,11 +605,11 @@ independent copies will be created::
h(x);
}
function g(uint[20] y) {
function g(uint[20] y) internal {
y[2] = 3;
}
function h(uint[20] storage y) {
function h(uint[20] storage y) internal {
y[3] = 4;
}
}

View File

@ -197,17 +197,16 @@ for the two input parameters and two returned values.
pragma solidity ^0.4.0;
/** @title Shape calculator.*/
contract shapeCalculator{
/**@dev Calculates a rectangle's surface and perimeter.
* @param w Width of the rectangle.
* @param h Height of the rectangle.
* @return s The calculated surface.
* @return p The calculated perimeter.
*/
function rectangle(uint w, uint h) returns (uint s, uint p) {
s = w * h;
p = 2 * (w + h);
}
}
/** @title Shape calculator. */
contract shapeCalculator {
/** @dev Calculates a rectangle's surface and perimeter.
* @param w Width of the rectangle.
* @param h Height of the rectangle.
* @return s The calculated surface.
* @return p The calculated perimeter.
*/
function rectangle(uint w, uint h) returns (uint s, uint p) {
s = w * h;
p = 2 * (w + h);
}
}