Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wa-lang
wa
提交
6f3da4c8
wa
项目概览
wa-lang
/
wa
大约 1 年 前同步成功
通知
68
Star
655
Fork
45
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
wa
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6f3da4c8
编写于
8月 05, 2023
作者:
chai2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
math/bits 包补充测试
上级
9c615cf0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
104 addition
and
161 deletion
+104
-161
waroot/src/math/bits/example_test.wa
waroot/src/math/bits/example_test.wa
+104
-48
waroot/src/math/bits/make_examples.go
waroot/src/math/bits/make_examples.go
+0
-113
未找到文件。
waroot/src/math/bits/example_test.wa
浏览文件 @
6f3da4c8
// 版权 @2023 凹语言 作者。保留所有权利。
func
Example
LeadingZeros8 {
func
Test
LeadingZeros8 {
assert(0b00000001 == 1)
println("LeadingZeros8(00000001) =", LeadingZeros8(1))
// Output:
// LeadingZeros8(00000001) = 7
assert(7 == LeadingZeros8(1))
}
func
Example
LeadingZeros16 {
func
Test
LeadingZeros16 {
assert(0b0000000000000001 == 1)
println("LeadingZeros16(0000000000000001) =", LeadingZeros16(1))
// Output:
// LeadingZeros16(0000000000000001) = 15
assert(15 == LeadingZeros16(1))
}
func
Example
LeadingZeros32 {
func
Test
LeadingZeros32 {
assert(0b00000000000000000000000000000001 == 1)
println("LeadingZeros32(00000000000000000000000000000001) =", LeadingZeros32(1))
// Output:
// LeadingZeros32(00000000000000000000000000000001) = 31
assert(31 == LeadingZeros32(1))
}
func
Example
LeadingZeros64 {
func
Test
LeadingZeros64 {
assert(0b0000000000000000000000000000000000000000000000000000000000000001 == 1)
println("LeadingZeros64(0000000000000000000000000000000000000000000000000000000000000001) =", LeadingZeros64(1))
// Output:
// LeadingZeros64(0000000000000000000000000000000000000000000000000000000000000001) = 63
assert(63 == LeadingZeros64(1))
}
func
Example
TrailingZeros8 {
func
Test
TrailingZeros8 {
assert(0b00001110 == 14)
println("TrailingZeros8(00001110) =", TrailingZeros8(14))
// Output:
// TrailingZeros8(00001110) = 1
assert(1 == TrailingZeros8(14))
}
func
Example
TrailingZeros16 {
func
Test
TrailingZeros16 {
assert(0b0000000000001110 == 14)
println("TrailingZeros16(0000000000001110) =", TrailingZeros16(14))
// Output:
// TrailingZeros16(0000000000001110) = 1
assert(1 == TrailingZeros16(14))
}
func
Example
TrailingZeros32 {
func
Test
TrailingZeros32 {
assert(0b00000000000000000000000000001110 == 14)
println("TrailingZeros32(00000000000000000000000000001110) =", TrailingZeros32(14))
// Output:
// TrailingZeros32(00000000000000000000000000001110) = 1
assert(1 == TrailingZeros32(14))
}
func
Example
TrailingZeros64 {
func
Test
TrailingZeros64 {
assert(0b0000000000000000000000000000000000000000000000000000000000001110 == 14)
println("TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001110) =", TrailingZeros64(14))
// Output:
// TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001110) = 1
assert(1 == TrailingZeros64(14))
}
func
Example
OnesCount8 {
func
Test
OnesCount8 {
assert(0b00001110 == 14)
println("OnesCount8(00001110) =", OnesCount8(14))
// Output:
// OnesCount8(00001110) = 3
assert(3 == OnesCount8(14))
}
func
Example
OnesCount16 {
func
Test
OnesCount16 {
assert(0b0000000000001110 == 14)
println("OnesCount16(0000000000001110) =", OnesCount16(14))
// Output:
// OnesCount16(0000000000001110) = 3
assert(3 == OnesCount16(14))
}
func
Example
OnesCount32 {
func
Test
OnesCount32 {
assert(0b00000000000000000000000000001110 == 14)
println("OnesCount32(00000000000000000000000000001110) =", OnesCount32(14))
// Output:
// OnesCount32(00000000000000000000000000001110) = 3
assert(3 == OnesCount32(14))
}
func
Example
OnesCount64 {
func
Test
OnesCount64 {
assert(0b0000000000000000000000000000000000000000000000000000000000001110 == 14)
println("OnesCount64(0000000000000000000000000000000000000000000000000000000000001110) =", OnesCount64(14))
// Output:
// OnesCount64(0000000000000000000000000000000000000000000000000000000000001110) = 3
assert(3 == OnesCount64(14))
}
func TestRotateLeft8 {
assert(0b00001111 == 15)
assert(0b00111100 == RotateLeft8(15, 2))
assert(0b11000011 == RotateLeft8(15, -2))
}
func TestRotateLeft16 {
assert(0b0000000000001111 == 15)
assert(0b0000000000111100 == RotateLeft16(15, 2))
assert(0b1100000000000011 == RotateLeft16(15, -2))
}
func TestRotateLeft32 {
assert(0b00000000000000000000000000001111 == 15)
assert(0b00000000000000000000000000111100 == RotateLeft32(15, 2))
assert(0b11000000000000000000000000000011 == RotateLeft32(15, -2))
}
func TestRotateLeft64 {
assert(0b0000000000000000000000000000000000000000000000000000000000001111 == 15)
assert(0b0000000000000000000000000000000000000000000000000000000000111100 == RotateLeft64(15, 2))
assert(0b1100000000000000000000000000000000000000000000000000000000000011 == RotateLeft64(15, -2))
}
func TestReverse8 {
assert(0b00010011 == 19)
assert(0b11001000 == Reverse8(19))
}
func TestReverse16 {
assert(0b0000000000010011 == 19)
assert(0b1100100000000000 == Reverse16(19))
}
func TestReverse32 {
assert(0b00000000000000000000000000010011 == 19)
assert(0b11001000000000000000000000000000 == Reverse32(19))
}
func TestReverse64 {
assert(0b0000000000000000000000000000000000000000000000000000000000010011 == 19)
assert(0b1100100000000000000000000000000000000000000000000000000000000000 == Reverse64(19))
}
func TestReverseBytes16 {
assert(0b0000000000001111 == 15)
assert(0b0000111100000000 == ReverseBytes16(15))
}
func TestReverseBytes32 {
assert(0b00000000000000000000000000001111 == 15)
assert(0b00001111000000000000000000000000 == ReverseBytes32(15))
}
func TestReverseBytes64 {
assert(0b0000000000000000000000000000000000000000000000000000000000001111 == 15)
assert(0b0000111100000000000000000000000000000000000000000000000000000000 == ReverseBytes64(15))
}
func TestLen8 {
assert(0b00001000 == 8)
assert(4 == Len8(8))
}
func TestLen16 {
assert(0b0000000000001000 == 8)
assert(4 == Len16(8))
}
func TestLen32 {
assert(0b00000000000000000000000000001000 == 8)
assert(4 == Len32(8))
}
func TestLen64 {
assert(0b0000000000000000000000000000000000000000000000000000000000001000 == 8)
assert(4 == Len64(8))
}
waroot/src/math/bits/make_examples.go
已删除
100644 → 0
浏览文件 @
9c615cf0
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
// This program generates example_test.go.
package
main
import
(
"bytes"
"fmt"
"log"
"math/bits"
"os"
)
const
header
=
`// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by go run make_examples.go. DO NOT EDIT.
package bits_test
import (
"fmt"
"math/bits"
)
`
func
main
()
{
w
:=
bytes
.
NewBuffer
([]
byte
(
header
))
for
_
,
e
:=
range
[]
struct
{
name
string
in
int
out
[
4
]
interface
{}
out2
[
4
]
interface
{}
}{
{
name
:
"LeadingZeros"
,
in
:
1
,
out
:
[
4
]
interface
{}{
bits
.
LeadingZeros8
(
1
),
bits
.
LeadingZeros16
(
1
),
bits
.
LeadingZeros32
(
1
),
bits
.
LeadingZeros64
(
1
)},
},
{
name
:
"TrailingZeros"
,
in
:
14
,
out
:
[
4
]
interface
{}{
bits
.
TrailingZeros8
(
14
),
bits
.
TrailingZeros16
(
14
),
bits
.
TrailingZeros32
(
14
),
bits
.
TrailingZeros64
(
14
)},
},
{
name
:
"OnesCount"
,
in
:
14
,
out
:
[
4
]
interface
{}{
bits
.
OnesCount8
(
14
),
bits
.
OnesCount16
(
14
),
bits
.
OnesCount32
(
14
),
bits
.
OnesCount64
(
14
)},
},
{
name
:
"RotateLeft"
,
in
:
15
,
out
:
[
4
]
interface
{}{
bits
.
RotateLeft8
(
15
,
2
),
bits
.
RotateLeft16
(
15
,
2
),
bits
.
RotateLeft32
(
15
,
2
),
bits
.
RotateLeft64
(
15
,
2
)},
out2
:
[
4
]
interface
{}{
bits
.
RotateLeft8
(
15
,
-
2
),
bits
.
RotateLeft16
(
15
,
-
2
),
bits
.
RotateLeft32
(
15
,
-
2
),
bits
.
RotateLeft64
(
15
,
-
2
)},
},
{
name
:
"Reverse"
,
in
:
19
,
out
:
[
4
]
interface
{}{
bits
.
Reverse8
(
19
),
bits
.
Reverse16
(
19
),
bits
.
Reverse32
(
19
),
bits
.
Reverse64
(
19
)},
},
{
name
:
"ReverseBytes"
,
in
:
15
,
out
:
[
4
]
interface
{}{
nil
,
bits
.
ReverseBytes16
(
15
),
bits
.
ReverseBytes32
(
15
),
bits
.
ReverseBytes64
(
15
)},
},
{
name
:
"Len"
,
in
:
8
,
out
:
[
4
]
interface
{}{
bits
.
Len8
(
8
),
bits
.
Len16
(
8
),
bits
.
Len32
(
8
),
bits
.
Len64
(
8
)},
},
}
{
for
i
,
size
:=
range
[]
int
{
8
,
16
,
32
,
64
}
{
if
e
.
out
[
i
]
==
nil
{
continue
// function doesn't exist
}
f
:=
fmt
.
Sprintf
(
"%s%d"
,
e
.
name
,
size
)
fmt
.
Fprintf
(
w
,
"
\n
func Example%s() {
\n
"
,
f
)
switch
e
.
name
{
case
"RotateLeft"
,
"Reverse"
,
"ReverseBytes"
:
fmt
.
Fprintf
(
w
,
"
\t
fmt.Printf(
\"
%%0%db
\\
n
\"
, %d)
\n
"
,
size
,
e
.
in
)
if
e
.
name
==
"RotateLeft"
{
fmt
.
Fprintf
(
w
,
"
\t
fmt.Printf(
\"
%%0%db
\\
n
\"
, bits.%s(%d, 2))
\n
"
,
size
,
f
,
e
.
in
)
fmt
.
Fprintf
(
w
,
"
\t
fmt.Printf(
\"
%%0%db
\\
n
\"
, bits.%s(%d, -2))
\n
"
,
size
,
f
,
e
.
in
)
}
else
{
fmt
.
Fprintf
(
w
,
"
\t
fmt.Printf(
\"
%%0%db
\\
n
\"
, bits.%s(%d))
\n
"
,
size
,
f
,
e
.
in
)
}
fmt
.
Fprintf
(
w
,
"
\t
// Output:
\n
"
)
fmt
.
Fprintf
(
w
,
"
\t
// %0*b
\n
"
,
size
,
e
.
in
)
fmt
.
Fprintf
(
w
,
"
\t
// %0*b
\n
"
,
size
,
e
.
out
[
i
])
if
e
.
name
==
"RotateLeft"
&&
e
.
out2
[
i
]
!=
nil
{
fmt
.
Fprintf
(
w
,
"
\t
// %0*b
\n
"
,
size
,
e
.
out2
[
i
])
}
default
:
fmt
.
Fprintf
(
w
,
"
\t
fmt.Printf(
\"
%s(%%0%db) = %%d
\\
n
\"
, %d, bits.%s(%d))
\n
"
,
f
,
size
,
e
.
in
,
f
,
e
.
in
)
fmt
.
Fprintf
(
w
,
"
\t
// Output:
\n
"
)
fmt
.
Fprintf
(
w
,
"
\t
// %s(%0*b) = %d
\n
"
,
f
,
size
,
e
.
in
,
e
.
out
[
i
])
}
fmt
.
Fprintf
(
w
,
"}
\n
"
)
}
}
if
err
:=
os
.
WriteFile
(
"example_test.go"
,
w
.
Bytes
(),
0666
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录