Source file
src/math/bits/make_examples.go
1
2
3
4
5
6
7
8
9
10 package main
11
12 import (
13 "bytes"
14 "fmt"
15 "log"
16 "math/bits"
17 "os"
18 )
19
20 const header = `// Copyright 2017 The Go Authors. All rights reserved.
21 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
23
24 // Code generated by go run make_examples.go. DO NOT EDIT.
25
26 package bits_test
27
28 import (
29 "fmt"
30 "math/bits"
31 )
32 `
33
34 func main() {
35 w := bytes.NewBuffer([]byte(header))
36
37 for _, e := range []struct {
38 name string
39 in int
40 out [4]any
41 out2 [4]any
42 }{
43 {
44 name: "LeadingZeros",
45 in: 1,
46 out: [4]any{bits.LeadingZeros8(1), bits.LeadingZeros16(1), bits.LeadingZeros32(1), bits.LeadingZeros64(1)},
47 },
48 {
49 name: "TrailingZeros",
50 in: 14,
51 out: [4]any{bits.TrailingZeros8(14), bits.TrailingZeros16(14), bits.TrailingZeros32(14), bits.TrailingZeros64(14)},
52 },
53 {
54 name: "OnesCount",
55 in: 14,
56 out: [4]any{bits.OnesCount8(14), bits.OnesCount16(14), bits.OnesCount32(14), bits.OnesCount64(14)},
57 },
58 {
59 name: "RotateLeft",
60 in: 15,
61 out: [4]any{bits.RotateLeft8(15, 2), bits.RotateLeft16(15, 2), bits.RotateLeft32(15, 2), bits.RotateLeft64(15, 2)},
62 out2: [4]any{bits.RotateLeft8(15, -2), bits.RotateLeft16(15, -2), bits.RotateLeft32(15, -2), bits.RotateLeft64(15, -2)},
63 },
64 {
65 name: "Reverse",
66 in: 19,
67 out: [4]any{bits.Reverse8(19), bits.Reverse16(19), bits.Reverse32(19), bits.Reverse64(19)},
68 },
69 {
70 name: "ReverseBytes",
71 in: 15,
72 out: [4]any{nil, bits.ReverseBytes16(15), bits.ReverseBytes32(15), bits.ReverseBytes64(15)},
73 },
74 {
75 name: "Len",
76 in: 8,
77 out: [4]any{bits.Len8(8), bits.Len16(8), bits.Len32(8), bits.Len64(8)},
78 },
79 } {
80 for i, size := range []int{8, 16, 32, 64} {
81 if e.out[i] == nil {
82 continue
83 }
84 f := fmt.Sprintf("%s%d", e.name, size)
85 fmt.Fprintf(w, "\nfunc Example%s() {\n", f)
86 switch e.name {
87 case "RotateLeft", "Reverse", "ReverseBytes":
88 fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", %d)\n", size, e.in)
89 if e.name == "RotateLeft" {
90 fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d, 2))\n", size, f, e.in)
91 fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d, -2))\n", size, f, e.in)
92 } else {
93 fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d))\n", size, f, e.in)
94 }
95 fmt.Fprintf(w, "\t// Output:\n")
96 fmt.Fprintf(w, "\t// %0*b\n", size, e.in)
97 fmt.Fprintf(w, "\t// %0*b\n", size, e.out[i])
98 if e.name == "RotateLeft" && e.out2[i] != nil {
99 fmt.Fprintf(w, "\t// %0*b\n", size, e.out2[i])
100 }
101 default:
102 fmt.Fprintf(w, "\tfmt.Printf(\"%s(%%0%db) = %%d\\n\", %d, bits.%s(%d))\n", f, size, e.in, f, e.in)
103 fmt.Fprintf(w, "\t// Output:\n")
104 fmt.Fprintf(w, "\t// %s(%0*b) = %d\n", f, size, e.in, e.out[i])
105 }
106 fmt.Fprintf(w, "}\n")
107 }
108 }
109
110 if err := os.WriteFile("example_test.go", w.Bytes(), 0666); err != nil {
111 log.Fatal(err)
112 }
113 }
114
View as plain text