func testUint(t *testing.T, n int) { values := make([]uint, n) for i := range values { values[i] = uint(rand.Int()) } var buf bytes.Buffer w := bit.NewWriter(&buf) for _, v := range values { WriteUint(w, v) } err := w.Close() if err != nil { t.Errorf("w.Close: %v", err) return } r := bit.NewReader(&buf) for i, exp := range values { got, err := ReadUint(r), r.Error() if err != nil { t.Errorf("ReadUint: %v", err) return } if got != exp { t.Errorf("%v: %d got %v expected %v", values, i, got, exp) return } } }
func main() { flag.Parse() filename := flag.Arg(0) for i := range byteCount { byteCount[i] = bit.Count(uint64(i)) } data, err := ioutil.ReadFile(filename) check(err) bitcount := 0 for _, b := range data { bitcount += byteCount[b] } fmt.Printf("bit bias: %.4f\n", float64(bitcount)/float64(len(data)*8)) r := bit.NewReader(bytes.NewReader(data)) circle := byte(0) count2 := make(map[byte]int) count3 := make(map[byte]int) count4 := make(map[byte]int) count5 := make(map[byte]int) count6 := make(map[byte]int) count7 := make(map[byte]int) count8 := make(map[byte]int) var v int for r.Error() == nil { v = r.ReadBit() circle <<= 1 circle |= byte(v) count2[circle&(1<<2-1)] += 1 count3[circle&(1<<3-1)] += 1 count4[circle&(1<<4-1)] += 1 count5[circle&(1<<5-1)] += 1 count6[circle&(1<<6-1)] += 1 count7[circle&(1<<7-1)] += 1 count8[circle&(1<<8-1)] += 1 } PrintFrequency("2", count2, data) PrintFrequency("3", count3, data) PrintFrequency("4", count4, data) PrintFrequency("5", count5, data) PrintFrequency("6", count6, data) PrintFrequency("7", count7, data) PrintFrequency("8", count8, data) }