1+ /*******************************************************************************
2+ * ___ _ ____ ____
3+ * / _ \ _ _ ___ ___| |_| _ \| __ )
4+ * | | | | | | |/ _ \/ __| __| | | | _ \
5+ * | |_| | |_| | __/\__ \ |_| |_| | |_) |
6+ * \__\_\\__,_|\___||___/\__|____/|____/
7+ *
8+ * Copyright (c) 2014-2019 Appsicle
9+ * Copyright (c) 2019-2024 QuestDB
10+ *
11+ * Licensed under the Apache License, Version 2.0 (the "License");
12+ * you may not use this file except in compliance with the License.
13+ * You may obtain a copy of the License at
14+ *
15+ * http://www.apache.org/licenses/LICENSE-2.0
16+ *
17+ * Unless required by applicable law or agreed to in writing, software
18+ * distributed under the License is distributed on an "AS IS" BASIS,
19+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+ * See the License for the specific language governing permissions and
21+ * limitations under the License.
22+ *
23+ ******************************************************************************/
24+
25+ using System . Text ;
26+ using NUnit . Framework ;
27+ using QuestDB . Enums ;
28+
29+ namespace net_questdb_client_tests ;
30+
31+ internal static class DecimalTestHelpers
32+ {
33+ internal static void AssertDecimalField ( ReadOnlySpan < byte > buffer ,
34+ string columnName ,
35+ byte expectedScale ,
36+ ReadOnlySpan < byte > expectedMantissa )
37+ {
38+ var payload = ExtractDecimalPayload ( buffer , columnName ) ;
39+ Assert . That ( payload . Length ,
40+ Is . GreaterThanOrEqualTo ( 4 + expectedMantissa . Length ) ,
41+ $ "Decimal field `{ columnName } ` payload shorter than expected.") ;
42+ Assert . That ( payload [ 0 ] , Is . EqualTo ( ( byte ) '=' ) ) ;
43+ Assert . That ( payload [ 1 ] , Is . EqualTo ( ( byte ) BinaryFormatType . DECIMAL ) ) ;
44+ Assert . That ( payload [ 2 ] , Is . EqualTo ( expectedScale ) , $ "Unexpected scale for `{ columnName } `.") ;
45+ Assert . That ( payload [ 3 ] ,
46+ Is . EqualTo ( ( byte ) expectedMantissa . Length ) ,
47+ $ "Unexpected mantissa length for `{ columnName } `.") ;
48+ CollectionAssert . AreEqual ( expectedMantissa . ToArray ( ) , payload . Slice ( 4 , expectedMantissa . Length ) . ToArray ( ) ,
49+ $ "Mantissa bytes for `{ columnName } ` did not match expectation.") ;
50+ }
51+
52+ internal static void AssertDecimalNullField ( ReadOnlySpan < byte > buffer , string columnName )
53+ {
54+ var payload = ExtractDecimalPayload ( buffer , columnName ) ;
55+ Assert . That ( payload . Length ,
56+ Is . GreaterThanOrEqualTo ( 4 ) ,
57+ $ "Decimal field `{ columnName } ` payload shorter than expected.") ;
58+ Assert . That ( payload [ 0 ] , Is . EqualTo ( ( byte ) '=' ) ) ;
59+ Assert . That ( payload [ 1 ] , Is . EqualTo ( ( byte ) BinaryFormatType . DECIMAL ) ) ;
60+ Assert . That ( payload [ 2 ] , Is . EqualTo ( 0 ) , $ "Unexpected scale for `{ columnName } `.") ;
61+ Assert . That ( payload [ 3 ] , Is . EqualTo ( 0 ) , $ "Unexpected mantissa length for `{ columnName } `.") ;
62+ }
63+
64+ private static ReadOnlySpan < byte > ExtractDecimalPayload ( ReadOnlySpan < byte > buffer , string columnName )
65+ {
66+ var prefix = Encoding . ASCII . GetBytes ( $ "{ columnName } =") ;
67+ var index = buffer . IndexOf ( prefix . AsSpan ( ) ) ;
68+ Assert . That ( index , Is . GreaterThanOrEqualTo ( 0 ) , $ "Column `{ columnName } ` not found in payload.") ;
69+ return buffer [ ( index + prefix . Length ) ..] ;
70+ }
71+ }
0 commit comments