1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 // This program generates example_test.go.
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.
24 // Code generated by go run make_examples.go. DO NOT EDIT.
35 w
:= bytes
.NewBuffer([]byte(header
))
37 for _
, e
:= range []struct {
46 out
: [4]any
{bits
.LeadingZeros8(1), bits
.LeadingZeros16(1), bits
.LeadingZeros32(1), bits
.LeadingZeros64(1)},
49 name
: "TrailingZeros",
51 out
: [4]any
{bits
.TrailingZeros8(14), bits
.TrailingZeros16(14), bits
.TrailingZeros32(14), bits
.TrailingZeros64(14)},
56 out
: [4]any
{bits
.OnesCount8(14), bits
.OnesCount16(14), bits
.OnesCount32(14), bits
.OnesCount64(14)},
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)},
67 out
: [4]any
{bits
.Reverse8(19), bits
.Reverse16(19), bits
.Reverse32(19), bits
.Reverse64(19)},
72 out
: [4]any
{nil, bits
.ReverseBytes16(15), bits
.ReverseBytes32(15), bits
.ReverseBytes64(15)},
77 out
: [4]any
{bits
.Len8(8), bits
.Len16(8), bits
.Len32(8), bits
.Len64(8)},
80 for i
, size
:= range []int{8, 16, 32, 64} {
82 continue // function doesn't exist
84 f
:= fmt
.Sprintf("%s%d", e
.name
, size
)
85 fmt
.Fprintf(w
, "\nfunc Example%s() {\n", f
)
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
)
93 fmt
.Fprintf(w
, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d))\n", size
, f
, e
.in
)
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
])
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
])
106 fmt
.Fprintf(w
, "}\n")
110 if err
:= os
.WriteFile("example_test.go", w
.Bytes(), 0666); err
!= nil {