whisper: add utility functions for creating topics

This commit is contained in:
Péter Szilágyi
2015-04-14 11:12:09 +03:00
parent cb707ba50c
commit 4af7743663
3 changed files with 58 additions and 8 deletions

View File

@ -15,10 +15,39 @@ var topicCreationTests = []struct {
}
func TestTopicCreation(t *testing.T) {
// Create the topics individually
for i, tt := range topicCreationTests {
topic := NewTopic(tt.data)
if bytes.Compare(topic[:], tt.hash[:]) != 0 {
t.Errorf("test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
t.Errorf("binary test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
}
}
for i, tt := range topicCreationTests {
topic := NewTopicFromString(string(tt.data))
if bytes.Compare(topic[:], tt.hash[:]) != 0 {
t.Errorf("textual test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
}
}
// Create the topics in batches
binaryData := make([][]byte, len(topicCreationTests))
for i, tt := range topicCreationTests {
binaryData[i] = tt.data
}
textualData := make([]string, len(topicCreationTests))
for i, tt := range topicCreationTests {
textualData[i] = string(tt.data)
}
topics := NewTopics(binaryData...)
for i, tt := range topicCreationTests {
if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
t.Errorf("binary batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
}
}
topics = NewTopicsFromStrings(textualData...)
for i, tt := range topicCreationTests {
if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
t.Errorf("textual batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
}
}
}