Commit e98df816 authored by Tristan Claverie's avatar Tristan Claverie

Merge branch '189_sign_sequence_generation' into 'master'

189 sign sequence generation

Introduce sequence generation into the platform.

See merge request !36
parents d836a2cd 146d3d33
Pipeline #355 passed with stages
package contract
import (
"dfss/dfssp/entities"
)
// GenerateSignSequence for the contract signature
//
// The generated sequence is an array of integers refering to the User array.
func GenerateSignSequence(users []*entities.User) []int {
return SquaredSignEngine(len(users))
}
// SquaredSignEngine is a basic ^2 engine for sequence generation
func SquaredSignEngine(n int) []int {
sequence := make([]int, n*n)
for i := 0; i < n; i++ {
for k := 0; k < n; k++ {
sequence[i*n+k] = k
}
}
return sequence
}
// SquaredSignEngineSlice is the same as the above with slicing
func SquaredSignEngineSlice(n int) []int {
baseSequence := make([]int, n)
// populate base slice
for i := 0; i < n; i++ {
baseSequence[i] = i
}
sequence := make([]int, 0, n*n)
// append n-1 time the slice to itself
for i := 0; i < n; i++ {
sequence = append(sequence, baseSequence...)
}
return sequence
}
package contract_test
import (
"dfss/dfssp/contract"
"dfss/dfssp/entities"
"github.com/bmizerany/assert"
"testing"
)
var refSeq = []int{0, 1, 2, 0, 1, 2, 0, 1, 2} // for n = 3
func TestGenerateSignSequence(t *testing.T) {
// initialise fixtures
var users = make([]*entities.User, 3)
users[0] = entities.NewUser()
users[1] = entities.NewUser()
users[2] = entities.NewUser()
users[0].Email = "user1@example.com"
users[1].Email = "user2@example.com"
users[2].Email = "user3@example.com"
assert.Equal(t, refSeq, contract.GenerateSignSequence(users))
}
// Perform sequence generation in a loop
func BenchmarkSquaredEngine(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = contract.SquaredSignEngine(10)
}
}
// Perform sequence generation in a loop with slicing
func BenchmarkSquaredEngineSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = contract.SquaredSignEngineSlice(10)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment