Skip to content

Commit f701d8f

Browse files
committed
value_tests: add tests for CqlVarint normalization
1 parent 62acc15 commit f701d8f

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

scylla-cql/src/frame/value_tests.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::types::serialize::{CellWriter, RowWriter};
77

88
use super::response::result::{ColumnSpec, ColumnType, TableSpec};
99
use super::value::{
10-
CqlDate, CqlDuration, CqlTime, CqlTimestamp, LegacyBatchValues, LegacySerializedValues,
11-
MaybeUnset, SerializeValuesError, Unset, Value, ValueList, ValueTooBig,
10+
CqlDate, CqlDuration, CqlTime, CqlTimestamp, CqlVarint, LegacyBatchValues,
11+
LegacySerializedValues, MaybeUnset, SerializeValuesError, Unset, Value, ValueList, ValueTooBig,
1212
};
1313
use bigdecimal::BigDecimal;
1414
use bytes::BufMut;
@@ -81,6 +81,47 @@ fn counter_serialization() {
8181
);
8282
}
8383

84+
fn cql_varint_normalization_test_cases() -> [(Vec<u8>, Vec<u8>); 11] {
85+
[
86+
(vec![], vec![0x00]), // 0
87+
(vec![0x00], vec![0x00]), // 0
88+
(vec![0x00, 0x00], vec![0x00]), // 0
89+
(vec![0x01], vec![0x01]), // 1
90+
(vec![0x00, 0x01], vec![0x01]), // 1
91+
(vec![0x7f], vec![0x7f]), // 127
92+
(vec![0x00, 0x7f], vec![0x7f]), // 127
93+
(vec![0x80], vec![0x80]), // -128
94+
(vec![0x00, 0x80], vec![0x00, 0x80]), // 128
95+
(vec![0xff], vec![0xff]), // -1
96+
(vec![0x00, 0xff], vec![0x00, 0xff]), // 255
97+
]
98+
}
99+
100+
#[test]
101+
fn cql_varint_normalization() {
102+
let test_cases = cql_varint_normalization_test_cases();
103+
104+
for test in test_cases {
105+
let non_normalized = CqlVarint::from_signed_bytes_be(test.0);
106+
let normalized = CqlVarint::from_signed_bytes_be(test.1);
107+
108+
assert_eq!(non_normalized, normalized);
109+
assert_eq!(compute_hash(&non_normalized), compute_hash(&normalized));
110+
}
111+
}
112+
113+
#[test]
114+
fn cql_varint_normalization_with_bigint() {
115+
let test_cases = cql_varint_normalization_test_cases();
116+
117+
for test in test_cases {
118+
let non_normalized: BigInt = CqlVarint::from_signed_bytes_be(test.0).into();
119+
let normalized: BigInt = CqlVarint::from_signed_bytes_be(test.1).into();
120+
121+
assert_eq!(non_normalized, normalized);
122+
}
123+
}
124+
84125
#[test]
85126
fn bigint_serialization() {
86127
let cases_from_the_spec: &[(i64, Vec<u8>)] = &[

0 commit comments

Comments
 (0)