-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type parameters and performance improvements (#1)
- Loading branch information
Showing
11 changed files
with
502 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) 2022, Janoš Guljaš <[email protected]> | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package schulze | ||
|
||
type bitSet []uint64 | ||
|
||
func newBitset(size uint) bitSet { | ||
return bitSet(make([]uint64, size/64+1)) | ||
} | ||
|
||
func (s bitSet) set(i uint) { | ||
s[i/64] |= 1 << (i % 64) | ||
} | ||
|
||
func (s bitSet) isSet(i uint) bool { | ||
return s[i/64]&(1<<(i%64)) != 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2022, Janoš Guljaš <[email protected]> | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package schulze | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBitSet(t *testing.T) { | ||
contains := func(i uint, values []uint) bool { | ||
for _, v := range values { | ||
if i == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
seed := time.Now().UnixNano() | ||
r := rand.New(rand.NewSource(seed)) | ||
size := uint(r.Intn(12345)) | ||
s := newBitset(size) | ||
values := make([]uint, 0) | ||
for i, count := uint(0), uint(r.Intn(100)); i < count; i++ { | ||
values = append(values, uint(r.Intn(int(size)))) | ||
} | ||
for _, v := range values { | ||
s.set(v) | ||
} | ||
for i := uint(0); i < size; i++ { | ||
if contains(i, values) { | ||
if !s.isSet(i) { | ||
t.Errorf("expected value %v is not set (seed %v)", i, seed) | ||
} | ||
} else { | ||
if s.isSet(i) { | ||
t.Errorf("value %v is unexpectedly set (seed %v)", i, seed) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2022, Janoš Guljaš <[email protected]> | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package schulze_test | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"resenje.org/schulze" | ||
) | ||
|
||
func TestVoting_Vote_UnknownChoiceError(t *testing.T) { | ||
v := schulze.NewVoting([]int{0, 2, 5, 7}) | ||
|
||
err := v.Vote(schulze.Ballot[int]{20: 1}) | ||
var verr *schulze.UnknownChoiceError[int] | ||
if !errors.As(err, &verr) { | ||
t.Fatalf("got error %v, want UnknownChoiceError", err) | ||
} | ||
if verr.Choice != 20 { | ||
t.Fatalf("got unknown choice error choice %v, want %v", verr.Choice, 20) | ||
} | ||
if !strings.Contains(verr.Error(), "20") { | ||
t.Fatal("choice index not found in error string") | ||
} | ||
} | ||
|
||
func TestVote_UnknownChoiceError(t *testing.T) { | ||
choices := []int{0, 2, 5, 7} | ||
preferences := schulze.NewPreferences(len(choices)) | ||
|
||
err := schulze.Vote(choices, preferences, schulze.Ballot[int]{20: 1}) | ||
var verr *schulze.UnknownChoiceError[int] | ||
if !errors.As(err, &verr) { | ||
t.Fatalf("got error %v, want UnknownChoiceError", err) | ||
} | ||
if verr.Choice != 20 { | ||
t.Fatalf("got unknown choice error choice %v, want %v", verr.Choice, 20) | ||
} | ||
if !strings.Contains(verr.Error(), "20") { | ||
t.Fatal("choice index not found in error string") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module resenje.org/schulze | ||
|
||
go 1.16 | ||
go 1.19 |
Oops, something went wrong.