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