aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/topic_test.go
blob: 4015079dcf32521a54d041074509c4bd2ca13d7d (plain) (blame)
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
package whisper

import (
    "bytes"
    "testing"
)

var topicCreationTests = []struct {
    data []byte
    hash [4]byte
}{
    {hash: [4]byte{0xc5, 0xd2, 0x46, 0x01}, data: nil},
    {hash: [4]byte{0xc5, 0xd2, 0x46, 0x01}, data: []byte{}},
    {hash: [4]byte{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte("test name")},
}

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("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)
        }
    }
}

func TestTopicSetCreation(t *testing.T) {
    topics := make([]Topic, len(topicCreationTests))
    for i, tt := range topicCreationTests {
        topics[i] = NewTopic(tt.data)
    }
    set := newTopicSet(topics)
    for i, tt := range topicCreationTests {
        topic := NewTopic(tt.data)
        if _, ok := set[topic.String()]; !ok {
            t.Errorf("topic %d: not found in set", i)
        }
    }
}