1+ using SpacetimeDB ;
2+ using SpacetimeDB . BSATN ;
3+ using SpacetimeDB . Types ;
4+
5+ public class Tests
6+ {
7+ [ Fact ]
8+ public static void GenericEqualityComparerCheck ( )
9+ {
10+ // Validates the behavior of the GenericEqualityComparer's Equals function
11+
12+ // Byte Arrays
13+ byte [ ] byteArray = new byte [ 10 ] ;
14+ byte [ ] byteArrayByRef = byteArray ;
15+ byte [ ] byteArrayByValue = new byte [ 10 ] ;
16+ byte [ ] byteArrayUnequalValue = new byte [ 01 ] ;
17+
18+ Assert . True ( GenericEqualityComparer . Instance . Equals ( byteArray , byteArrayByRef ) ) ;
19+ Assert . True ( GenericEqualityComparer . Instance . Equals ( byteArray , byteArrayByValue ) ) ;
20+ Assert . False ( GenericEqualityComparer . Instance . Equals ( byteArray , byteArrayUnequalValue ) ) ;
21+
22+ // Integers
23+ int integer = 5 ;
24+ int integerByValue = 5 ;
25+ int integerUnequalValue = 7 ;
26+ string integerAsDifferingType = "5" ;
27+
28+ Assert . True ( GenericEqualityComparer . Instance . Equals ( integer , integerByValue ) ) ;
29+ Assert . False ( GenericEqualityComparer . Instance . Equals ( integer , integerUnequalValue ) ) ;
30+ // GenericEqualityComparer does not support to converting datatypes and will fail this test
31+ Assert . False ( GenericEqualityComparer . Instance . Equals ( integer , integerAsDifferingType ) ) ;
32+
33+ // String
34+ string testString = "This is a test" ;
35+ string testStringByRef = testString ;
36+ string testStringByValue = "This is a test" ;
37+ string testStringUnequalValue = "This is not the same string" ;
38+
39+ Assert . True ( GenericEqualityComparer . Instance . Equals ( testString , testStringByRef ) ) ;
40+ Assert . True ( GenericEqualityComparer . Instance . Equals ( testString , testStringByValue ) ) ;
41+ Assert . False ( GenericEqualityComparer . Instance . Equals ( testString , testStringUnequalValue ) ) ;
42+
43+ // Note: We are limited to only [SpacetimeDB.Type]
44+
45+ // Identity and User
46+ Identity identity = Identity . From ( Convert . FromBase64String ( "l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=" ) ) ;
47+ Identity identityByRef = identity ;
48+ Identity identityByValue = Identity . From ( Convert . FromBase64String ( "l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=" ) ) ;
49+ Identity identityUnequalValue = Identity . From ( Convert . FromBase64String ( "j5DMlKmWjfbSl7qmZQOok7HDSwsAJopRSJjdlUsNogs=" ) ) ;
50+
51+ User testUser = new User { Identity = identity , Name = "name" , Online = false } ;
52+ User testUserByRef = testUser ;
53+ User testUserByValue = new User { Identity = identity , Name = "name" , Online = false } ;
54+ User testUserUnequalIdentityValue = new User { Identity = identityUnequalValue , Name = "name" , Online = false } ;
55+ User testUserUnequalNameValue = new User { Identity = identity , Name = "unequalName" , Online = false } ;
56+ User testUserUnequalOnlineValue = new User { Identity = identity , Name = "name" , Online = true } ;
57+
58+ Assert . True ( GenericEqualityComparer . Instance . Equals ( identity , identityByRef ) ) ;
59+ Assert . True ( GenericEqualityComparer . Instance . Equals ( identity , identityByValue ) ) ;
60+ Assert . False ( GenericEqualityComparer . Instance . Equals ( identity , identityUnequalValue ) ) ;
61+
62+ Assert . True ( GenericEqualityComparer . Instance . Equals ( testUser , testUserByRef ) ) ;
63+ Assert . True ( GenericEqualityComparer . Instance . Equals ( testUser , testUserByValue ) ) ;
64+ Assert . False ( GenericEqualityComparer . Instance . Equals ( testUser , testUserUnequalIdentityValue ) ) ;
65+ Assert . False ( GenericEqualityComparer . Instance . Equals ( testUser , testUserUnequalNameValue ) ) ;
66+ Assert . False ( GenericEqualityComparer . Instance . Equals ( testUser , testUserUnequalOnlineValue ) ) ;
67+
68+ // TaggedEnum using Status record
69+ Status statusCommitted = new Status . Committed ( default ) ;
70+ Status statusCommittedByRef = statusCommitted ;
71+ Status statusCommittedByValue = new Status . Committed ( default ) ;
72+ Status statusFailed = new Status . Failed ( "Failed" ) ;
73+ Status statusFailedByValue = new Status . Failed ( "Failed" ) ;
74+ Status statusFailedUnequalValue = new Status . Failed ( "unequalFailed" ) ;
75+ Status statusOutOfEnergy = new Status . OutOfEnergy ( default ) ;
76+
77+ Assert . True ( GenericEqualityComparer . Instance . Equals ( statusCommitted , statusCommittedByRef ) ) ;
78+ Assert . True ( GenericEqualityComparer . Instance . Equals ( statusCommitted , statusCommittedByValue ) ) ;
79+ Assert . False ( GenericEqualityComparer . Instance . Equals ( statusCommitted , statusFailed ) ) ;
80+ Assert . True ( GenericEqualityComparer . Instance . Equals ( statusFailed , statusFailedByValue ) ) ;
81+ Assert . False ( GenericEqualityComparer . Instance . Equals ( statusFailed , statusFailedUnequalValue ) ) ;
82+ Assert . False ( GenericEqualityComparer . Instance . Equals ( statusCommitted , statusOutOfEnergy ) ) ;
83+ }
84+ }
0 commit comments