Skip to content

Commit

Permalink
Enable more examples in the documentation for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Jul 18, 2017
1 parent 01fbc63 commit cffe52d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
2 changes: 2 additions & 0 deletions docs/abi-spec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ For example,

::

pragma solidity ^0.4.0;

contract Test {
function Test(){ b = 0x12345678901234567890123456789012; }
event Event(uint indexed a, bytes32 b)
Expand Down
8 changes: 4 additions & 4 deletions docs/assembly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -509,7 +509,7 @@ case called ``default``.

.. code::
assembly {
{
let x := 0
switch calldataload(4)
case 0 {
Expand Down Expand Up @@ -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))
Expand All @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,8 @@ Interfaces are denoted by their own keyword:

::

pragma solidity ^0.4.11;

interface Token {
function transfer(address recipient, uint amount);
}
Expand Down
2 changes: 0 additions & 2 deletions docs/control-structures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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

Expand Down
33 changes: 25 additions & 8 deletions docs/frequently-asked-questions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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 {
...
// ...
}
}
}
Expand Down Expand Up @@ -363,6 +370,8 @@ of variable it concerns:

Example::

pragma solidity ^0.4.0;

contract C {
uint[] data1;
uint[] data2;
Expand All @@ -375,7 +384,7 @@ Example::
append(data2);
}

function append(uint[] storage d) {
function append(uint[] storage d) internal {
d.push(1);
}
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
}
Expand Down
27 changes: 13 additions & 14 deletions docs/layout-of-source-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit cffe52d

Please sign in to comment.