38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestFilter(t *testing.T) {
|
||
|
filter1 := []uint8{0b10101, 0b11111, 0b00000, 0b00111, 0b00101, 0b00001, 0b10111, 0b10101, 0b01101, 0b10101}
|
||
|
filter2 := []uint8{0b10101, 0b11111, 0b00000, 0b00111, 0b00101, 0b00001, 0b01000, 0b01010, 0b01000, 0b11011}
|
||
|
filterR := []uint8{0b10101, 0b11111, 0b00000, 0b00111, 0b00101, 0b00001, 0b00000, 0b00000, 0b01000, 0b10001}
|
||
|
|
||
|
for i, filterResult := range filterR {
|
||
|
resultByte := compareBytes(filter1[i], filter2[i])
|
||
|
if resultByte != filterResult {
|
||
|
t.Errorf("%b AND %b = %b; got %b\n", filter1[i], filter2[i], filterResult, resultByte)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func compareBytes(filter uint8, filterCheck uint8) uint8 {
|
||
|
filterQuoted := (filterCheck & 1) != 0 // first bit
|
||
|
filterPin := (filterCheck & 2) != 0 // second bit
|
||
|
filterReply := (filterCheck & 4) != 0 // third bit
|
||
|
filterRetweet := (filterCheck & 8) != 0 // fourth bit
|
||
|
filterSelfThread := (filterCheck & 16) != 0 // fifth bit
|
||
|
|
||
|
var tweetFilter uint8 = 0
|
||
|
filterMap := []bool{filterSelfThread, filterRetweet, filterReply, filterPin, filterQuoted}
|
||
|
for _, f := range filterMap {
|
||
|
tweetFilter <<= 1
|
||
|
if f {
|
||
|
tweetFilter |= 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return filter & tweetFilter
|
||
|
}
|