This repository was archived by the owner on Aug 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathBytesExamples.sol
More file actions
133 lines (99 loc) · 4.06 KB
/
BytesExamples.sol
File metadata and controls
133 lines (99 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
pragma solidity ^0.4.16;
pragma experimental "v0.5.0";
pragma experimental "ABIEncoderV2";
import {Bytes} from "../src/bytes/Bytes.sol";
contract BytesExamples {
using Bytes for *;
// Check if bytes are equal.
function bytesEqualsExample() public pure {
bytes memory bts0 = hex"01020304";
bytes memory bts1 = hex"01020304";
bytes memory bts2 = hex"05060708";
assert(bts0.equals(bts0)); // Check if a byte array equal to itself.
assert(bts0.equals(bts1)); // Should be equal because they have the same byte at each position.
assert(!bts0.equals(bts2)); // Should not be equal.
}
// Check reference equality
function bytesEqualsRefExample() public pure {
bytes memory bts0 = hex"01020304";
bytes memory bts1 = bts0;
// Every 'bytes' will satisfy 'equalsRef' with itself.
assert(bts0.equalsRef(bts0));
// Different variables, but bts0 was assigned to bts1, so they point to the same area in memory.
assert(bts0.equalsRef(bts1));
// Changing a byte in bts0 will also affect bts1.
bts0[2] = 0x55;
assert(bts1[2] == 0x55);
bytes memory bts2 = hex"01020304";
bytes memory bts3 = hex"01020304";
// These bytes has the same byte at each pos (so they would pass 'equals'), but they are referencing different areas in memory.
assert(!bts2.equalsRef(bts3));
// Changing a byte in bts2 will not affect bts3.
bts2[2] = 0x55;
assert(bts3[2] != 0x55);
}
// copying
function bytesCopyExample() public pure {
bytes memory bts0 = hex"01020304";
var bts0Copy0 = bts0.copy();
// The individual bytes are the same.
assert(bts0.equals(bts0Copy0));
// bts0Copy is indeed a (n independent) copy.
assert(!bts0.equalsRef(bts0Copy0));
bytes memory bts1 = hex"0304";
// Copy with start index.
var bts0Copy1 = bts0.copy(2);
assert(bts0Copy1.equals(bts1));
bytes memory bts2 = hex"0203";
// Copy with start index and length.
var bts0Copy2 = bts0.copy(1, 2);
assert(bts0Copy2.equals(bts2));
}
// concatenate
function bytesConcatExample() public pure {
bytes memory bts0 = hex"01020304";
bytes memory bts1 = hex"05060708";
bytes memory bts01 = hex"0102030405060708";
var cct = bts0.concat(bts1);
// Should be equal to bts01
assert(cct.equals(bts01));
}
// find the highest byte set in a bytes32
function bytes32HighestByteSetExample() public pure {
bytes32 test0 = 0x01;
bytes32 test1 = 0xbb00aa00;
bytes32 test2 = "abc";
// with bytesN, the highest byte is the least significant one.
assert(test0.highestByteSet() == 31);
assert(test1.highestByteSet() == 30); // aa
assert(test2.highestByteSet() == 2);
// Make sure that in the case of test2, the highest byte is equal to "c".
assert(test2[test2.highestByteSet()] == 0x63);
}
// find the lowest byte set in a bytes32
function bytes32LowestByteSetExample() public pure {
bytes32 test0 = 0x01;
bytes32 test1 = 0xbb00aa00;
bytes32 test2 = "abc";
// with bytesN, the lowest byte is the most significant one.
assert(test0.lowestByteSet() == 31);
assert(test1.lowestByteSet() == 28); // bb
assert(test2.lowestByteSet() == 0);
}
// find the highest byte set in a uint
function uintHighestByteSetExample() public pure {
uint test0 = 0x01;
uint test1 = 0xbb00aa00;
// with uint, the highest byte is the most significant one.
assert(test0.highestByteSet() == 0);
assert(test1.highestByteSet() == 3);
}
// find the lowest byte set in a uint
function uintLowestByteSetExample() public pure {
uint test0 = 0x01;
uint test1 = 0xbb00aa00;
// with uint, the lowest byte is the least significant one.
assert(test0.lowestByteSet() == 0);
assert(test1.lowestByteSet() == 1);
}
}