Skip to content

Commit

Permalink
Add the reverseRangeFunc & rangeFunc methods on ArrayUtils cont…
Browse files Browse the repository at this point in the history
…ract to support decreasing ranges (#17)

* Add the reverseRangeFunc and reverseRange methods on ArrayUtils contract to support decreasing ranges

* Add Cadence tests for ArrayUtils smart contract

* Replace the reverseRange function with reverse

The reverse function accepts an array as an argument, and returns
its reversed version.
  • Loading branch information
m-Peter authored May 18, 2023
1 parent e81f5a2 commit 2c8b180
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cadence/contracts/ArrayUtils.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@
pub contract ArrayUtils {

pub fun rangeFunc(_ start: Int, _ end: Int, _ f : ((Int):Void) ) {
pub fun rangeFunc(_ start: Int, _ end: Int, _ f: ((Int): Void)) {
var current = start
while current < end{
while current < end {
f(current)
current = current + 1
}
}

pub fun range(_ start: Int, _ end: Int): [Int]{
var res:[Int] = []
self.rangeFunc(start, end, fun (i:Int){
pub fun range(_ start: Int, _ end: Int): [Int] {
var res: [Int] = []
self.rangeFunc(start, end, fun (i: Int) {
res.append(i)
})
return res
}

pub fun reverse(_ array: [Int]): [Int] {
var res: [Int] = []
var i: Int = array.length - 1
while i >= 0 {
res.append(array[i])
i = i - 1
}
return res
}

pub fun transform(_ array: &[AnyStruct], _ f : ((AnyStruct): AnyStruct)){
for i in self.range(0, array.length){
array[i] = f(array[i])
Expand Down
135 changes: 135 additions & 0 deletions test/ArrayUtils_test.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import Test
import ArrayUtils from "../cadence/contracts/ArrayUtils.cdc"

pub struct Token {
pub let id: Int
pub(set) var balance: Int

init(id: Int, balance: Int) {
self.id = id
self.balance = balance
}
}

pub(set) var arrayUtils = ArrayUtils()

pub fun testRange() {
// Act
var range = arrayUtils.range(0, 10)

// Assert
var expected: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var i: Int = 0
while i < expected.length {
Test.assert(expected[i] == range[i])
i = i + 1
}
}

pub fun testReverseRange() {
// Act
var range = arrayUtils.reverse(arrayUtils.range(0, 10))

// Assert
var expected: [Int] = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
var i: Int = 0
while i < expected.length {
Test.assert(expected[i] == range[i])
i = i + 1
}
}

pub fun testTransform() {
// Arrange
let tokens = [
Token(id: 0, balance: 10),
Token(id: 1, balance: 5),
Token(id: 2, balance: 15)
]

// Act
arrayUtils.transform(&tokens as &[AnyStruct], fun (t: AnyStruct): AnyStruct {
var token = t as! Token
token.balance = token.balance * 2
return token
})

// Assert
Test.assert(tokens[0].balance == 20)
Test.assert(tokens[1].balance == 10)
Test.assert(tokens[2].balance == 30)
}

pub fun testIterate() {
// Arrange
let tokens = [
Token(id: 0, balance: 10),
Token(id: 1, balance: 5),
Token(id: 2, balance: 15),
Token(id: 3, balance: 22),
Token(id: 4, balance: 31)
]

// Act
arrayUtils.iterate(tokens as [AnyStruct], fun (t: AnyStruct): Bool {
var token = t as! Token
return token.id <= 2
})
}

pub fun testMap() {
// Arrange
let tokens = [
Token(id: 0, balance: 10),
Token(id: 1, balance: 5),
Token(id: 2, balance: 15)
]

// Act
let mapped = arrayUtils.map(tokens as [AnyStruct], fun (t: AnyStruct): AnyStruct {
var token = t as! Token
token.balance = token.balance - 2
return token
})

// Assert
Test.assert((mapped[0] as! Token).balance == 8)
Test.assert((mapped[1] as! Token).balance == 3)
Test.assert((mapped[2] as! Token).balance == 13)
}

pub fun testMapStrings() {
// Arrange
let strings = ["Peter", "John", "Mark"]

// Act
let mapped = arrayUtils.mapStrings(strings, fun (s: String): String {
return "Hello, ".concat(s).concat("!")
})

// Assert
Test.assert(mapped[0] == "Hello, Peter!")
Test.assert(mapped[1] == "Hello, John!")
Test.assert(mapped[2] == "Hello, Mark!")
}

pub fun testReduce() {
// Arrange
let tokens = [
Token(id: 0, balance: 10),
Token(id: 1, balance: 5),
Token(id: 2, balance: 15)
]
let initial = Token(id: 5, balance: 0) as AnyStruct

// Act
let token = arrayUtils.reduce(tokens as [AnyStruct], initial, fun (acc: AnyStruct, t: AnyStruct): AnyStruct {
var token = t as! Token
var accToken = acc as! Token
accToken.balance = accToken.balance + token.balance
return accToken
})

// Assert
Test.assert((token as! Token).balance == 30)
}

0 comments on commit 2c8b180

Please sign in to comment.