55
66namespace StackExchange . Redis . Tests ;
77
8- [ RunPerProtocol ]
8+ [ Collection ( NonParallelCollection . Name ) ] // because of the FP32 suppression
99public sealed class VectorSetUnitTests ( ITestOutputHelper output )
1010{
1111 // the aim of this test is to validate that we're sending the right thing - VADD is complex
1212 [ Theory ]
13- [ InlineData ( VectorSetQuantization . Int8 ) ]
14- [ InlineData ( VectorSetQuantization . None ) ]
15- [ InlineData ( VectorSetQuantization . Binary ) ]
16- public async Task VectorSetAdd_WithEverything ( VectorSetQuantization quantization )
13+ [ InlineData ( VectorSetQuantization . Int8 , false ) ]
14+ [ InlineData ( VectorSetQuantization . None , false ) ]
15+ [ InlineData ( VectorSetQuantization . Binary , false ) ]
16+ [ InlineData ( VectorSetQuantization . Int8 , true ) ]
17+ [ InlineData ( VectorSetQuantization . None , true ) ]
18+ [ InlineData ( VectorSetQuantization . Binary , true ) ]
19+ public async Task VectorSetAdd_WithEverything ( VectorSetQuantization quantization , bool disableFp32 )
1720 {
1821 using var server = new VectorServer ( output ) ;
1922 await using var conn = await server . ConnectAsync ( ) ;
@@ -25,20 +28,29 @@ public async Task VectorSetAdd_WithEverything(VectorSetQuantization quantization
2528 var vector = new [ ] { 1.0f , 2.0f , 3.0f , 4.0f } ;
2629 var attributes = """{"category":"test","id":123}""" ;
2730
28- var request = VectorSetAddRequest . Member (
29- "element1" ,
30- vector . AsMemory ( ) ,
31- attributes ) ;
32- request . Quantization = quantization ;
33- request . ReducedDimensions = 64 ;
34- request . BuildExplorationFactor = 300 ;
35- request . MaxConnections = 32 ;
36- request . UseCheckAndSet = true ;
37- output . WriteLine ( "Storing..." ) ;
38- var result = await db . VectorSetAddAsync (
39- key ,
40- request ) ;
41- Assert . True ( result ) ;
31+ try
32+ {
33+ if ( disableFp32 ) VectorSetAddMessage . SuppressFp32 ( ) ;
34+ Assert . Equal ( ! disableFp32 , VectorSetAddMessage . UseFp32 ) ;
35+ var request = VectorSetAddRequest . Member (
36+ "element1" ,
37+ vector . AsMemory ( ) ,
38+ attributes ) ;
39+ request . Quantization = quantization ;
40+ request . ReducedDimensions = 64 ;
41+ request . BuildExplorationFactor = 300 ;
42+ request . MaxConnections = 32 ;
43+ request . UseCheckAndSet = true ;
44+ output . WriteLine ( "Storing..." ) ;
45+ var result = await db . VectorSetAddAsync (
46+ key ,
47+ request ) ;
48+ Assert . True ( result ) ;
49+ }
50+ finally
51+ {
52+ if ( disableFp32 ) VectorSetAddMessage . RestoreFp32 ( ) ;
53+ }
4254
4355 // now: what did we send?
4456 var req = server . LastRequest . ReadRequest ( ) . AsSpan ( ) ;
@@ -48,17 +60,34 @@ public async Task VectorSetAdd_WithEverything(VectorSetQuantization quantization
4860 {
4961 output . WriteLine ( $ " $ '{ item } '") ;
5062 }
51- Assert . Equal ( quantization is VectorSetQuantization . Int8 ? 14 : 15 , req . Length ) ;
63+
5264 Assert . Equal ( "VADD" , req [ 0 ] ) ;
5365 Assert . Equal ( "mykey" , req [ 1 ] ) ;
5466 Assert . Equal ( "REDUCE" , req [ 2 ] ) ;
5567 Assert . Equal ( 64 , req [ 3 ] ) ;
56- Assert . Equal ( "FP32" , req [ 4 ] ) ;
57- Assert . Equal ( "00-00-80-3F-00-00-00-40-00-00-40-40-00-00-80-40" , BitConverter . ToString ( req [ 5 ] ! ) ) ;
58- Assert . Equal ( "element1" , req [ 6 ] ) ;
59- Assert . Equal ( "CAS" , req [ 7 ] ) ;
68+ req = req . Slice ( 4 ) ;
69+
70+ if ( disableFp32 )
71+ {
72+ Assert . Equal ( "VALUES" , req [ 0 ] ) ;
73+ Assert . Equal ( 4 , req [ 1 ] ) ;
74+ Assert . Equal ( 1.0f , ( float ) req [ 2 ] , precision : 3 ) ;
75+ Assert . Equal ( 2.0f , ( float ) req [ 3 ] , precision : 3 ) ;
76+ Assert . Equal ( 3.0f , ( float ) req [ 4 ] , precision : 3 ) ;
77+ Assert . Equal ( 4.0f , ( float ) req [ 5 ] , precision : 3 ) ;
78+ req = req . Slice ( 6 ) ;
79+ }
80+ else
81+ {
82+ Assert . Equal ( "FP32" , req [ 0 ] ) ;
83+ Assert . Equal ( "00-00-80-3F-00-00-00-40-00-00-40-40-00-00-80-40" , BitConverter . ToString ( req [ 1 ] ! ) ) ;
84+ req = req . Slice ( 2 ) ;
85+ }
86+
87+ Assert . Equal ( "element1" , req [ 0 ] ) ;
88+ Assert . Equal ( "CAS" , req [ 1 ] ) ;
89+ req = req . Slice ( 2 ) ;
6090
61- req = req . Slice ( 8 ) ;
6291 switch ( quantization )
6392 {
6493 case VectorSetQuantization . None :
@@ -70,12 +99,16 @@ public async Task VectorSetAdd_WithEverything(VectorSetQuantization quantization
7099 req = req . Slice ( 1 ) ;
71100 break ;
72101 }
102+
73103 Assert . Equal ( "EF" , req [ 0 ] ) ;
74104 Assert . Equal ( 300 , req [ 1 ] ) ;
75105 Assert . Equal ( "SETATTR" , req [ 2 ] ) ;
76106 Assert . Equal ( """{"category":"test","id":123}""" , req [ 3 ] ) ;
77107 Assert . Equal ( "M" , req [ 4 ] ) ;
78108 Assert . Equal ( 32 , req [ 5 ] ) ;
109+ req = req . Slice ( 6 ) ;
110+
111+ Assert . True ( req . IsEmpty ) ;
79112 }
80113
81114 private sealed class VectorServer ( ITestOutputHelper log ) : InProcessTestServer ( log )
0 commit comments