1- #![ no_std]
2- #![ no_main]
1+ #![ cfg_attr( not( any( test, feature = "testing" ) ) , no_std) ]
2+ #![ cfg_attr( not( any( test, feature = "testing" ) ) , no_main) ]
3+ #![ cfg_attr( any( test, feature = "testing" ) , feature( thread_local) ) ]
34
45extern crate alloc;
5- use alloc:: vec:: Vec ;
6+
7+ #[ cfg( any( test, feature = "testing" ) ) ]
8+ extern crate std;
9+
610use alloc:: string:: { String , ToString } ;
711use amadeus_sdk:: * ;
812
@@ -36,8 +40,8 @@ impl Leaderboard {
3640 * self . total_matches += 1 ;
3741 }
3842
39- pub fn get_total_matches ( & self ) -> Vec < u8 > {
40- ( * self . total_matches ) . to_string ( ) . into_bytes ( )
43+ pub fn get_total_matches ( & self ) -> i32 {
44+ * self . total_matches
4145 }
4246
4347 pub fn record_match ( & mut self , player : String , match_id : u64 , score : u16 , opponent : String ) {
@@ -50,13 +54,13 @@ impl Leaderboard {
5054 * self . total_matches += 1 ;
5155 }
5256
53- pub fn get_match_score ( & self , player : String , match_id : u64 ) -> Vec < u8 > {
57+ pub fn get_match_score ( & self , player : String , match_id : u64 ) -> u16 {
5458 if let Some ( matches) = self . players . get ( player) {
5559 if let Some ( m) = matches. get ( match_id) {
56- return ( * m. score ) . to_string ( ) . into_bytes ( ) ;
60+ return * m. score ;
5761 }
5862 }
59- "0" . to_string ( ) . into_bytes ( )
63+ 0
6064 }
6165
6266 pub fn get_match_opponent ( & self , player : String , match_id : u64 ) -> String {
@@ -77,8 +81,8 @@ impl Leaderboard {
7781 ( * self . tournament . name ) . clone ( )
7882 }
7983
80- pub fn get_tournament_prize ( & self ) -> Vec < u8 > {
81- ( * self . tournament . prize_pool ) . to_string ( ) . into_bytes ( )
84+ pub fn get_tournament_prize ( & self ) -> u64 {
85+ * self . tournament . prize_pool
8286 }
8387
8488 pub fn record_win ( & mut self , player : String ) {
@@ -89,15 +93,98 @@ impl Leaderboard {
8993 }
9094 }
9195
92- pub fn get_player_wins ( & self , player : String ) -> Vec < u8 > {
96+ pub fn get_player_wins ( & self , player : String ) -> u32 {
9397 if let Some ( wins) = self . player_wins . get ( & player) {
94- ( * wins) . to_string ( ) . into_bytes ( )
98+ * * wins
9599 } else {
96- "0" . to_string ( ) . into_bytes ( )
100+ 0
97101 }
98102 }
99103
100104 pub fn set_player_wins ( & mut self , player : String , wins : u32 ) {
101105 self . player_wins . insert ( player, wins) ;
102106 }
103107}
108+
109+ #[ cfg( test) ]
110+ mod tests {
111+ use super :: * ;
112+ use amadeus_sdk:: testing:: * ;
113+
114+ #[ test]
115+ fn test_increment_total_matches ( ) {
116+ reset ( ) ;
117+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
118+ state. increment_total_matches ( ) ;
119+ state. flush ( ) ;
120+ println ! ( "\n {}\n " , dump( ) ) ;
121+ let state2 = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
122+ assert_eq ! ( state2. get_total_matches( ) , 1 ) ;
123+ }
124+
125+ #[ test]
126+ fn test_set_tournament_info ( ) {
127+ reset ( ) ;
128+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
129+ state. set_tournament_info ( "World Cup" . to_string ( ) , 1000000 ) ;
130+ state. flush ( ) ;
131+ println ! ( "\n {}\n " , dump( ) ) ;
132+ let state2 = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
133+ assert_eq ! ( state2. get_tournament_name( ) , "World Cup" ) ;
134+ assert_eq ! ( state2. get_tournament_prize( ) , 1000000 ) ;
135+ }
136+
137+ #[ test]
138+ fn test_record_win ( ) {
139+ reset ( ) ;
140+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
141+ state. record_win ( "alice" . to_string ( ) ) ;
142+ state. flush ( ) ;
143+ println ! ( "\n {}\n " , dump( ) ) ;
144+ let state2 = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
145+ assert_eq ! ( state2. get_player_wins( "alice" . to_string( ) ) , 1 ) ;
146+ }
147+
148+ #[ test]
149+ fn test_multiple_operations ( ) {
150+ reset ( ) ;
151+
152+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
153+ state. increment_total_matches ( ) ;
154+ state. flush ( ) ;
155+ println ! ( "After increment_total_matches():\n {}\n " , dump( ) ) ;
156+
157+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
158+ state. set_tournament_info ( "World Cup" . to_string ( ) , 1000000 ) ;
159+ state. flush ( ) ;
160+ println ! ( "After set_tournament_info():\n {}\n " , dump( ) ) ;
161+
162+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
163+ state. record_win ( "alice" . to_string ( ) ) ;
164+ state. flush ( ) ;
165+ println ! ( "After record_win(alice):\n {}\n " , dump( ) ) ;
166+
167+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
168+ state. record_win ( "alice" . to_string ( ) ) ;
169+ state. flush ( ) ;
170+ println ! ( "After record_win(alice) 2nd:\n {}\n " , dump( ) ) ;
171+
172+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
173+ state. record_win ( "bob" . to_string ( ) ) ;
174+ state. flush ( ) ;
175+ println ! ( "After record_win(bob):\n {}\n " , dump( ) ) ;
176+
177+ let mut state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
178+ state. set_player_wins ( "charlie" . to_string ( ) , 5 ) ;
179+ state. flush ( ) ;
180+ println ! ( "After set_player_wins(charlie, 5):\n {}\n " , dump( ) ) ;
181+
182+ let state = Leaderboard :: with_prefix ( Vec :: new ( ) ) ;
183+ assert_eq ! ( state. get_total_matches( ) , 1 ) ;
184+ assert_eq ! ( state. get_player_wins( "alice" . to_string( ) ) , 2 ) ;
185+ assert_eq ! ( state. get_player_wins( "bob" . to_string( ) ) , 1 ) ;
186+ assert_eq ! ( state. get_player_wins( "charlie" . to_string( ) ) , 5 ) ;
187+ assert_eq ! ( state. get_tournament_name( ) , "World Cup" ) ;
188+ assert_eq ! ( state. get_tournament_prize( ) , 1000000 ) ;
189+ }
190+ }
0 commit comments