1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools: make v test-cleancode test everything by default (#10050)

This commit is contained in:
Delyan Angelov
2021-05-08 13:32:29 +03:00
committed by GitHub
parent cba2cb6b9c
commit 8a380f4699
132 changed files with 3230 additions and 3440 deletions

View File

@ -55,7 +55,7 @@ pub fn encode_str(data string) string {
// alloc_and_encode is a private function that allocates and encodes data into a string
// Used by encode and encode_str
fn alloc_and_encode(src byteptr, len int) string {
fn alloc_and_encode(src &byte, len int) string {
size := 4 * ((len + 2) / 3)
if size <= 0 {
return ''
@ -107,7 +107,7 @@ pub fn url_encode_str(data string) string {
// Please note: The `buffer` should be large enough (i.e. 3/4 of the data.len, or larger)
// to hold the decoded data.
// Please note: This function does NOT allocate new memory, and is thus suitable for handling very large strings.
pub fn decode_in_buffer(data &string, buffer byteptr) int {
pub fn decode_in_buffer(data &string, buffer &byte) int {
mut padding := 0
if data.ends_with('=') {
if data.ends_with('==') {
@ -125,8 +125,8 @@ pub fn decode_in_buffer(data &string, buffer byteptr) int {
mut b := &byte(0)
mut d := &byte(0)
unsafe {
d = byteptr(data.str)
b = byteptr(buffer)
d = &byte(data.str)
b = &byte(buffer)
}
for i < input_length {
mut char_a := 0
@ -165,7 +165,7 @@ pub fn decode_in_buffer(data &string, buffer byteptr) int {
// encode_in_buffer returns the size of the encoded data in the buffer.
// Please note: The buffer should be large enough (i.e. 4/3 of the data.len, or larger) to hold the encoded data.
// Please note: The function does NOT allocate new memory, and is suitable for handling very large strings.
pub fn encode_in_buffer(data []byte, buffer byteptr) int {
pub fn encode_in_buffer(data []byte, buffer &byte) int {
return encode_from_buffer(buffer, data.data, data.len)
}
@ -173,16 +173,16 @@ pub fn encode_in_buffer(data []byte, buffer byteptr) int {
// and write the bytes into `dest`.
// Please note: The `dest` buffer should be large enough (i.e. 4/3 of the src_len, or larger) to hold the encoded data.
// Please note: This function is for internal base64 encoding
fn encode_from_buffer(dest byteptr, src byteptr, src_len int) int {
fn encode_from_buffer(dest &byte, src &byte, src_len int) int {
input_length := src_len
output_length := 4 * ((input_length + 2) / 3)
mut i := 0
mut j := 0
mut d := src
mut b := dest
mut etable := byteptr(base64.enc_table.str)
mut d := unsafe { src }
mut b := unsafe { dest }
mut etable := base64.enc_table.str
for i < input_length {
mut octet_a := 0
mut octet_b := 0

View File

@ -6,12 +6,11 @@ struct TestPair {
}
const (
pairs = [
pairs = [
// RFC 3548 examples
TestPair{'\x14\xfb\x9c\x03\xd9\x7e', 'FPucA9l+'},
TestPair{'\x14\xfb\x9c\x03\xd9', 'FPucA9k='},
TestPair{'\x14\xfb\x9c\x03', 'FPucAw=='},
// RFC 4648 examples
TestPair{'', ''},
TestPair{'f', 'Zg=='},
@ -20,7 +19,6 @@ const (
TestPair{'foob', 'Zm9vYg=='},
TestPair{'fooba', 'Zm9vYmE='},
TestPair{'foobar', 'Zm9vYmFy'},
// Wikipedia examples
TestPair{'sure.', 'c3VyZS4='},
TestPair{'sure', 'c3VyZQ=='},
@ -32,11 +30,7 @@ const (
TestPair{'sure.', 'c3VyZS4='},
]
man_pair = TestPair{
'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.',
'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='
}
man_pair = TestPair{'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.', 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='}
)
fn test_decode() {
@ -50,7 +44,7 @@ fn test_decode() {
for i, p in pairs {
got := base64.decode(p.encoded)
if got != p.decoded.bytes() {
eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}')
eprintln('pairs[$i]: expected = $p.decoded, got = $got')
assert false
}
}
@ -67,7 +61,7 @@ fn test_decode_str() {
for i, p in pairs {
got := base64.decode_str(p.encoded)
if got != p.decoded {
eprintln('pairs[${i}]: expected = ${p.decoded}, got = ${got}')
eprintln('pairs[$i]: expected = $p.decoded, got = $got')
assert false
}
}
@ -79,7 +73,7 @@ fn test_encode() {
for i, p in pairs {
got := base64.encode(p.decoded.bytes())
if got != p.encoded {
eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}')
eprintln('pairs[$i]: expected = $p.encoded, got = $got')
assert false
}
}
@ -91,7 +85,7 @@ fn test_encode_str() {
for i, p in pairs {
got := base64.encode_str(p.decoded)
if got != p.encoded {
eprintln('pairs[${i}]: expected = ${p.encoded}, got = ${got}')
eprintln('pairs[$i]: expected = $p.encoded, got = $got')
assert false
}
}
@ -108,31 +102,31 @@ fn test_url_encode_str() {
}
fn test_url_decode() {
test := base64.url_decode("SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ")
test := base64.url_decode('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ')
assert test == 'Hello Base64Url encoding!'.bytes()
}
fn test_url_decode_str() {
test := base64.url_decode_str("SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ")
test := base64.url_decode_str('SGVsbG8gQmFzZTY0VXJsIGVuY29kaW5nIQ')
assert test == 'Hello Base64Url encoding!'
}
fn test_encode_null_byte() {
assert base64.encode([byte(`A`) 0 `C`]) == 'QQBD'
assert base64.encode([byte(`A`), 0, `C`]) == 'QQBD'
}
fn test_encode_null_byte_str() {
// While this works, bytestr() does a memcpy
s := [byte(`A`) 0 `C`].bytestr()
s := [byte(`A`), 0, `C`].bytestr()
assert base64.encode_str(s) == 'QQBD'
}
fn test_decode_null_byte() {
assert base64.decode('QQBD') == [byte(`A`) 0 `C`]
assert base64.decode('QQBD') == [byte(`A`), 0, `C`]
}
fn test_decode_null_byte_str() {
// While this works, bytestr() does a memcpy
s := [byte(`A`) 0 `C`].bytestr()
s := [byte(`A`), 0, `C`].bytestr()
assert base64.decode_str('QQBD') == s
}

View File

@ -2,98 +2,99 @@
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module binary
// Little Endian
[inline]
pub fn little_endian_u16(b []byte) u16 {
_ = b[1] // bounds check
return u16(b[0]) | (u16(b[1])<<u16(8))
return u16(b[0]) | (u16(b[1]) << u16(8))
}
[inline]
pub fn little_endian_put_u16(mut b []byte, v u16) {
_ = b[1] // bounds check
b[0] = byte(v)
b[1] = byte(v>>u16(8))
b[1] = byte(v >> u16(8))
}
[inline]
pub fn little_endian_u32(b []byte) u32 {
_ = b[3] // bounds check
return u32(b[0]) | (u32(b[1])<<u32(8)) | (u32(b[2])<<u32(16)) | (u32(b[3])<<u32(24))
return u32(b[0]) | (u32(b[1]) << u32(8)) | (u32(b[2]) << u32(16)) | (u32(b[3]) << u32(24))
}
[inline]
pub fn little_endian_put_u32(mut b []byte, v u32) {
_ = b[3] // bounds check
b[0] = byte(v)
b[1] = byte(v>>u32(8))
b[2] = byte(v>>u32(16))
b[3] = byte(v>>u32(24))
b[1] = byte(v >> u32(8))
b[2] = byte(v >> u32(16))
b[3] = byte(v >> u32(24))
}
[inline]
pub fn little_endian_u64(b []byte) u64 {
_ = b[7] // bounds check
return u64(b[0]) | (u64(b[1])<<u64(8)) | (u64(b[2])<<u64(16)) | (u64(b[3])<<u64(24)) | (u64(b[4])<<u64(32)) | (u64(b[5])<<u64(40)) | (u64(b[6])<<u64(48)) | (u64(b[7])<<u64(56))
return u64(b[0]) | (u64(b[1]) << u64(8)) | (u64(b[2]) << u64(16)) | (u64(b[3]) << u64(24)) | (u64(b[4]) << u64(32)) | (u64(b[5]) << u64(40)) | (u64(b[6]) << u64(48)) | (u64(b[7]) << u64(56))
}
[inline]
pub fn little_endian_put_u64(mut b []byte, v u64) {
_ = b[7] // bounds check
b[0] = byte(v)
b[1] = byte(v>>u64(8))
b[2] = byte(v>>u64(16))
b[3] = byte(v>>u64(24))
b[4] = byte(v>>u64(32))
b[5] = byte(v>>u64(40))
b[6] = byte(v>>u64(48))
b[7] = byte(v>>u64(56))
b[1] = byte(v >> u64(8))
b[2] = byte(v >> u64(16))
b[3] = byte(v >> u64(24))
b[4] = byte(v >> u64(32))
b[5] = byte(v >> u64(40))
b[6] = byte(v >> u64(48))
b[7] = byte(v >> u64(56))
}
// Big Endian
[inline]
pub fn big_endian_u16(b []byte) u16 {
_ = b[1] // bounds check
return u16(b[1]) | (u16(b[0])<<u16(8))
return u16(b[1]) | (u16(b[0]) << u16(8))
}
[inline]
pub fn big_endian_put_u16(mut b []byte, v u16) {
_ = b[1] // bounds check
b[0] = byte(v>>u16(8))
b[0] = byte(v >> u16(8))
b[1] = byte(v)
}
[inline]
pub fn big_endian_u32(b []byte) u32 {
_ = b[3] // bounds check
return u32(b[3]) | (u32(b[2])<<u32(8)) | (u32(b[1])<<u32(16)) | (u32(b[0])<<u32(24))
return u32(b[3]) | (u32(b[2]) << u32(8)) | (u32(b[1]) << u32(16)) | (u32(b[0]) << u32(24))
}
[inline]
pub fn big_endian_put_u32(mut b []byte, v u32) {
_ = b[3] // bounds check
b[0] = byte(v>>u32(24))
b[1] = byte(v>>u32(16))
b[2] = byte(v>>u32(8))
b[0] = byte(v >> u32(24))
b[1] = byte(v >> u32(16))
b[2] = byte(v >> u32(8))
b[3] = byte(v)
}
[inline]
pub fn big_endian_u64(b []byte) u64 {
_ = b[7] // bounds check
return u64(b[7]) | (u64(b[6])<<u64(8))| (u64(b[5])<<u64(16)) | (u64(b[4])<<u64(24)) | (u64(b[3])<<u64(32)) | (u64(b[2])<<u64(40)) | (u64(b[1])<<u64(48)) | (u64(b[0])<<u64(56))
return u64(b[7]) | (u64(b[6]) << u64(8)) | (u64(b[5]) << u64(16)) | (u64(b[4]) << u64(24)) | (u64(b[3]) << u64(32)) | (u64(b[2]) << u64(40)) | (u64(b[1]) << u64(48)) | (u64(b[0]) << u64(56))
}
[inline]
pub fn big_endian_put_u64(mut b []byte, v u64) {
_ = b[7] // bounds check
b[0] = byte(v>>u64(56))
b[1] = byte(v>>u64(48))
b[2] = byte(v>>u64(40))
b[3] = byte(v>>u64(32))
b[4] = byte(v>>u64(24))
b[5] = byte(v>>u64(16))
b[6] = byte(v>>u64(8))
b[0] = byte(v >> u64(56))
b[1] = byte(v >> u64(48))
b[2] = byte(v >> u64(40))
b[3] = byte(v >> u64(32))
b[4] = byte(v >> u64(24))
b[5] = byte(v >> u64(16))
b[6] = byte(v >> u64(8))
b[7] = byte(v)
}

View File

@ -5,9 +5,7 @@ fn test_encoding_csv_reader() {
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'name'
@ -35,9 +33,7 @@ fn test_line_break_lf() {
mut csv_reader := csv.new_reader(lf_data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'name'
@ -55,9 +51,7 @@ fn test_line_break_cr() {
mut csv_reader := csv.new_reader(cr_data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'name'
@ -75,9 +69,7 @@ fn test_line_break_crlf() {
mut csv_reader := csv.new_reader(crlf_data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'name'
@ -95,9 +87,7 @@ fn test_no_line_ending() {
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
csv_reader.read() or {
break
}
csv_reader.read() or { break }
row_count++
}
assert row_count == 2
@ -108,9 +98,7 @@ fn test_last_field_empty() {
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'name'
@ -133,9 +121,7 @@ fn test_empty_line() {
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'name'
@ -160,9 +146,7 @@ fn test_field_multiple_line() {
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'name'
@ -182,9 +166,7 @@ fn test_field_quotes_for_parts() {
mut csv_reader := csv.new_reader(data)
mut row_count := 0
for {
row := csv_reader.read() or {
break
}
row := csv_reader.read() or { break }
row_count++
if row_count == 1 {
assert row[0] == 'a1'