How to use assert_change method in avocado

Best Python code snippet using avocado_python

test_rule.py

Source:test_rule.py Github

copy

Full Screen

...19 return Deck(cards, pvector(suits), pvector(values))20 def assert_no_change(self, state1: GameState, state2: GameState, *args: str):21 for key in args:22 self.assertEqual(state1[key], state2[key])23 def assert_change(self, state: GameState, **kwargs):24 for key, value in kwargs.items():25 self.assertEqual(value, state[key])26 def assert_change_in_lp_dp_mc(self, state1: GameState, state2: GameState, move: Move):27 if move.cards:28 self.assertEqual(len(state1.lp[state1.i]) - len(move.cards), len(state2.lp[state1.i]))29 self.assertFalse(any(c in state2.lp[state1.i] for c in move.cards))30 self.assertTrue(all(c in state2.lp[state1.i] for c in state1.lp[state1.i] if c not in move.cards))31 self.assertEqual(state1.mc + 1, state2.mc)32 if self.called == 0:33 self.assertEqual(state1.dp.extend(move.cards), state2.dp)34 def assert_and_make_legal_moves(self, count: int, state: GameState, moves: List[Move]) -> List[Move]:35 legal_moves: List[Move] = [mv for mv in moves if mv.test(state)]36 self.assertEqual(count, len(legal_moves))37 return legal_moves38 def test_match_suit_small(self):39 deck: Deck = self.create_deck([Value(val) for val in range(Value.VII, Value.UNDER)], [Suit.HEART])40 cards_list: List[Card] = list(deck.cards)41 cards_list.sort()42 moves: List[Move] = match_suit_rule.generate_moves(deck, 2)43 self.assertEqual(4, len(moves))44 self.assertTrue(all(len(mv.cards) == 1 for mv in moves))45 self.assertTrue(all(mv.suit == Suit.HEART for mv in moves))46 state1 = GameState(47 dp=cards_list[0:1],48 st=cards_list[1:2],49 lp=(Hand(cards_list[2:3]), Hand(cards_list[3:])),50 ace=0,51 suit=Suit.HEART,52 val=cards_list[0].value,53 cnt=1,54 i=0,55 mc=0,56 lp_mc=(-1, -1),57 deck_size=len(deck.cards)58 )59 legal_moves: List[Move] = self.assert_and_make_legal_moves(1, state1, moves)60 next_mv = legal_moves[0]61 state2 = next_mv.apply(state1)62 self.assert_no_change(state1, state2, 'st', 'ace', 'suit', 'cnt')63 self.assert_change_in_lp_dp_mc(state1, state2, next_mv)64 self.assert_change(65 state2,66 val=next_mv.cards[0].value,67 i=1,68 lp_mc=[1, -1]69 )70 s_evolver = state1.evolver()71 s_evolver.ace = 172 s_evolver.cnt = 073 self.assert_and_make_legal_moves(0, cast(GameState, s_evolver.persistent()), moves)74 s_evolver = state1.evolver()75 s_evolver.cnt = 376 self.assert_and_make_legal_moves(0, cast(GameState, s_evolver.persistent()), moves)77 def test_match_suit_medium(self):78 deck: Deck = self.create_deck([Value(val) for val in (Value.VII, Value.IX, Value.ACE)],79 [Suit(suit) for suit in (Suit.HEART, Suit.BELL)])80 cards_list: List[Card] = list(deck.cards)81 # HEART < BELL82 cards_list.sort()83 moves: List[Move] = match_suit_rule.generate_moves(deck, 2)84 self.assertEqual(2 * (3 + 3), len(moves))85 self.assertTrue(all(1 <= len(mv.cards) <= 2 for mv in moves))86 self.assertTrue(all(mv.suit == Suit.HEART or mv.suit == Suit.BELL for mv in moves))87 top = cards_list[0] # (HEART, VII)88 state1 = GameState(89 dp=[top],90 st=cards_list[1:2], # (HEART, IX)91 lp=(Hand(cards_list[2:-1]), # (HEART, ACE), (BELL, VII), (BELL, IX)92 Hand(cards_list[-1:])), # (BELL, ACE)93 ace=0,94 suit=top.suit,95 val=top.value,96 cnt=1,97 i=0,98 mc=0,99 lp_mc=(-1, -1),100 deck_size=len(deck.cards)101 )102 legal_moves: List[Move] = self.assert_and_make_legal_moves(1, state1, moves)103 next_mv = legal_moves[0] # PLAY (HEART, ACE)104 state2 = next_mv.apply(state1)105 self.assert_no_change(state1, state2, 'st', 'suit', 'lp_mc')106 self.assert_change_in_lp_dp_mc(state1, state2, next_mv)107 self.assert_change(108 state2,109 ace=1,110 val=next_mv.cards[0].value,111 i=1,112 cnt=0113 )114 self.assert_and_make_legal_moves(0, state2, moves)115 s_evolver = state1.evolver()116 s_evolver.lp = state1.lp.set(0, Hand(cards_list[2:-2] + [top])) # (HEART, VII), (HEART, ACE), (BELL, VII)117 s_evolver.cnt = 3118 state1 = cast(GameState, s_evolver.persistent())119 legal_moves: List[Move] = self.assert_and_make_legal_moves(2, state1, moves)120 next_mv = next(mv for mv in legal_moves if len(mv.cards) > 1) # PLAY (HEART, VII), (BELL, VII)121 state2 = next_mv.apply(state1)122 self.assert_no_change(state1, state2, 'ace', 'val', 'lp_mc')123 self.assert_change_in_lp_dp_mc(state1, state2, next_mv)124 self.assertEqual(next_mv.cards[-1], state2.dp[-1])125 self.assert_change(126 state2,127 suit=next_mv.cards[-1].suit,128 cnt=9,129 i=1130 )131 def test_match_suit_big(self):132 deck: Deck = GERMAN_CARDS_DECK133 cards_list: List[Card] = list(deck.cards)134 cards_list.sort()135 moves: List[Move] = match_suit_rule.generate_moves(deck, 4)136 self.assertEqual(4 * (8 * 3 * 2 * 1 + 8 * 3 * 2 + 8 * 3 + 8), len(moves))137 top = Card(Suit.HEART, Value.VIII)138 state1 = GameState(139 dp=[top],140 st=[Card(Suit.HEART, Value.KING), Card(Suit.HEART, Value.ACE), Card(Suit.ACORN, Value.UNDER),141 Card(Suit.ACORN, Value.OVER), Card(Suit.ACORN, Value.KING), Card(Suit.ACORN, Value.ACE),142 Card(Suit.LEAF, Value.VII), Card(Suit.LEAF, Value.VIII), Card(Suit.LEAF, Value.X),143 Card(Suit.LEAF, Value.UNDER), Card(Suit.LEAF, Value.OVER), Card(Suit.BELL, Value.VIII),144 Card(Suit.BELL, Value.X), Card(Suit.BELL, Value.UNDER), Card(Suit.BELL, Value.OVER),145 Card(Suit.BELL, Value.KING), Card(Suit.BELL, Value.ACE), Card(Suit.HEART, Value.OVER),146 Card(Suit.ACORN, Value.VIII)],147 lp=(Hand([Card(Suit.HEART, Value.VII), Card(Suit.ACORN, Value.VII)]),148 Hand([Card(Suit.LEAF, Value.KING), Card(Suit.LEAF, Value.ACE), Card(Suit.BELL, Value.VII)]),149 Hand([]),150 Hand([Card(Suit.HEART, Value.IX), Card(Suit.HEART, Value.X), Card(Suit.HEART, Value.UNDER),151 Card(Suit.BELL, Value.IX), Card(Suit.LEAF, Value.IX), Card(Suit.ACORN, Value.IX),152 Card(Suit.ACORN, Value.X)])),153 ace=0,154 suit=top.suit,155 val=top.value,156 cnt=1,157 i=3,158 mc=11,159 lp_mc=(-1, -1, 4, -1),160 deck_size=len(deck.cards)161 )162 # 3 + 1*3*2*1 + 1*3*2 + 1*3 + 1*1 = 19163 legal_moves1: List[Move] = self.assert_and_make_legal_moves(19, state1, moves)164 next_mv1: Move = next(mv for mv in legal_moves1 if len(mv.cards) == 4 and mv.cards[-1].suit == Suit.ACORN)165 state2: GameState = next_mv1.apply(state1)166 self.assert_no_change(state1, state2, 'st', 'ace', 'cnt', 'i', 'lp_mc')167 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)168 self.assert_change(169 state2,170 suit=Suit.ACORN,171 val=Value.IX172 )173 legal_moves2: List[Move] = self.assert_and_make_legal_moves(2, state2, moves)174 next_mv2: Move = next(mv for mv in legal_moves2 if len(mv.cards) == 2)175 state3: GameState = next_mv2.apply(state2)176 self.assert_no_change(state2, state3, 'st', 'ace', 'cnt', 'lp_mc')177 self.assert_change_in_lp_dp_mc(state2, state3, next_mv2)178 self.assert_change(179 state3,180 suit=Suit.HEART,181 val=Value.X,182 i=0183 )184 legal_moves3: List[Move] = self.assert_and_make_legal_moves(2, state3, moves)185 next_mv3: Move = next(mv for mv in legal_moves3 if len(mv.cards) == 2)186 state4: GameState = next_mv3.apply(state3)187 self.assert_no_change(state3, state4, 'st', 'ace', )188 self.assert_change_in_lp_dp_mc(state3, state4, next_mv3)189 self.assert_change(190 state4,191 suit=Suit.ACORN,192 val=Value.VII,193 cnt=6,194 i=1,195 lp_mc=[14, -1, 4, -1]196 )197 self.assert_and_make_legal_moves(0, state4, moves)198 def test_match_value_small(self):199 deck: Deck = self.create_deck([Value.VIII], SUITS)200 cards_list: List[Card] = list(deck.cards)201 cards_list.sort()202 moves: List[Move] = match_value_rule.generate_moves(deck, 2)203 # 4*3*2 + 4*3 + 4 = 64204 self.assertEqual(40, len(moves))205 top = Card(Suit.HEART, Value.VIII)206 state1 = GameState(207 dp=[top],208 st=[Card(Suit.BELL, Value.VIII)],209 lp=(Hand([Card(Suit.LEAF, Value.VIII)]), Hand([Card(Suit.ACORN, Value.VIII)])),210 ace=0,211 suit=top.suit,212 val=top.value,213 cnt=1,214 i=0,215 mc=0,216 lp_mc=(-1, -1),217 deck_size=len(deck.cards)218 )219 legal_moves: List[Move] = self.assert_and_make_legal_moves(1, state1, moves)220 next_mv = legal_moves[0]221 state2 = next_mv.apply(state1)222 self.assert_no_change(state1, state2, 'st', 'ace', 'val', 'cnt')223 # self.assert_change_in_lp(state1, state2, next_mv)224 self.assert_change(225 state2,226 dp=state1.dp.extend(next_mv.cards),227 suit=next_mv.cards[-1].suit,228 i=1,229 mc=1,230 lp_mc=[1, -1]231 )232 def test_match_value_medium(self):233 deck: Deck = self.create_deck([Value.VIII, Value.IX, Value.ACE], SUITS)234 cards_list: List[Card] = list(deck.cards)235 cards_list.sort()236 moves: List[Move] = match_value_rule.generate_moves(deck, 2)237 # 3 * (4*3*2 + 4*3 + 4) = 120238 self.assertEqual(120, len(moves))239 top = Card(Suit.HEART, Value.ACE)240 state1 = GameState(241 dp=[top],242 st=[Card(Suit.HEART, Value.VIII), Card(Suit.HEART, Value.IX), Card(Suit.BELL, Value.VIII),243 Card(Suit.LEAF, Value.VIII), Card(Suit.LEAF, Value.IX)],244 lp=(Hand([Card(Suit.LEAF, Value.ACE)]),245 Hand([Card(Suit.BELL, Value.ACE), Card(Suit.ACORN, Value.ACE), Card(Suit.ACORN, Value.VIII),246 Card(Suit.ACORN, Value.IX), Card(Suit.BELL, Value.IX)])),247 ace=0,248 suit=top.suit,249 val=top.value,250 cnt=1,251 i=1,252 mc=0,253 lp_mc=(-1, -1),254 deck_size=len(deck.cards)255 )256 legal_moves1: List[Move] = self.assert_and_make_legal_moves(4, state1, moves)257 next_mv1 = next(mv for mv in legal_moves1 if len(mv.cards) == 2 and mv.cards[-1].suit == Suit.ACORN)258 state2 = next_mv1.apply(state1)259 self.assert_no_change(state1, state2, 'st', 'val', 'lp_mc')260 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)261 self.assert_change(262 state2,263 ace=2,264 suit=next_mv1.suit,265 cnt=0,266 i=0,267 )268 s_evolver = state1.evolver()269 s_evolver.ace = 1270 s_evolver.cnt = 0271 state1b = cast(GameState, s_evolver.persistent())272 legal_moves1b: List[Move] = self.assert_and_make_legal_moves(4, state1b, moves)273 next_mv1b = next(mv for mv in legal_moves1b if len(mv.cards) == 2 and mv.cards[-1].suit == Suit.ACORN)274 state2b = next_mv1b.apply(state1b)275 self.assert_no_change(state2, state2b, 'st', 'dp', 'ace', 'suit', 'val', 'cnt', 'i', 'mc', 'lp_mc')276 self.assert_change_in_lp_dp_mc(state1, state2b, next_mv1b)277 legal_moves2: List[Move] = self.assert_and_make_legal_moves(1, state2, moves)278 next_mv2: Move = legal_moves2[0]279 state3: GameState = next_mv2.apply(state2)280 self.assert_no_change(state2, state3, 'st', 'val', 'ace', 'cnt')281 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)282 self.assert_change(283 state3,284 suit=next_mv2.suit,285 cnt=0,286 i=1,287 lp_mc=[2, -1]288 )289 self.assert_and_make_legal_moves(0, state3, moves)290 def test_play_over_small(self):291 deck: Deck = self.create_deck([Value.VIII, Value.OVER], [Suit.HEART, Suit.BELL])292 cards_list: List[Card] = list(deck.cards)293 cards_list.sort()294 moves: List[Move] = play_over_rule.generate_moves(deck, 2)295 self.assertEqual(8, len(moves))296 top = Card(Suit.HEART, Value.VIII)297 state1 = GameState(298 dp=[top],299 st=[Card(Suit.HEART, Value.OVER)],300 lp=(Hand([Card(Suit.BELL, Value.OVER)]),301 Hand([Card(Suit.BELL, Value.VIII)])),302 ace=0,303 suit=top.suit,304 val=top.value,305 cnt=1,306 i=0,307 mc=0,308 lp_mc=(-1, -1),309 deck_size=len(deck.cards)310 )311 legal_moves1: List[Move] = self.assert_and_make_legal_moves(2, state1, moves)312 next_mv1 = next(mv for mv in legal_moves1 if mv.suit == Suit.BELL)313 state2 = next_mv1.apply(state1)314 self.assert_no_change(state1, state2, 'st', 'ace', 'cnt')315 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)316 self.assert_change(317 state2,318 suit=next_mv1.suit,319 val=next_mv1.cards[0].value,320 i=1,321 lp_mc=[1, -1]322 )323 self.assert_and_make_legal_moves(0, state2, moves)324 def test_play_over_medium(self):325 deck: Deck = self.create_deck([Value.VIII, Value.OVER], SUITS)326 cards_list: List[Card] = list(deck.cards)327 cards_list.sort()328 moves: List[Move] = play_over_rule.generate_moves(deck, 2)329 # 4 * (4*3*2*1 + 4*3*2 + 4*3 + 4) = 256330 self.assertEqual(256, len(moves))331 top = Card(Suit.HEART, Value.VIII)332 state1 = GameState(333 dp=[top],334 st=[Card(Suit.ACORN, Value.VIII)],335 lp=(Hand([Card(Suit.LEAF, Value.OVER), Card(Suit.BELL, Value.OVER), Card(Suit.ACORN, Value.OVER),336 Card(Suit.BELL, Value.VIII)]),337 Hand([Card(Suit.HEART, Value.OVER), Card(Suit.LEAF, Value.VIII)])),338 ace=0,339 suit=top.suit,340 val=top.value,341 cnt=1,342 i=0,343 mc=0,344 lp_mc=(-1, -1),345 deck_size=len(deck.cards)346 )347 # 4 * (3*2*1 + 3*2 + 3) = 60348 legal_moves1: List[Move] = self.assert_and_make_legal_moves(60, state1, moves)349 next_mv1 = next(mv for mv in legal_moves1 if len(mv.cards) == 3 and mv.suit == Suit.HEART)350 state2 = next_mv1.apply(state1)351 self.assert_no_change(state1, state2, 'st', 'ace', 'suit', 'cnt', 'lp_mc')352 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)353 self.assert_change(354 state2,355 val=Value.OVER,356 i=1,357 )358 legal_moves2: List[Move] = self.assert_and_make_legal_moves(4, state2, moves)359 next_mv2: Move = legal_moves2[0]360 state3: GameState = next_mv2.apply(state2)361 self.assert_no_change(state1, state2, 'st', 'ace', 'cnt', 'lp_mc')362 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)363 self.assert_change(364 state3,365 suit=next_mv2.suit,366 cnt=1,367 i=0,368 lp_mc=[-1, -1]369 )370 self.assert_and_make_legal_moves(0, state3, moves)371 def test_draw_big(self):372 deck: Deck = GERMAN_CARDS_DECK373 cards_list: List[Card] = list(deck.cards)374 cards_list.sort()375 moves: List[Move] = DrawRule().generate_moves(deck, 4)376 self.assertEqual(3, len(moves))377 top = Card(Suit.HEART, Value.ACE)378 state1 = GameState(379 dp=[Card(Suit.LEAF, Value.ACE), Card(Suit.BELL, Value.ACE), top],380 st=[Card(Suit.HEART, Value.KING), Card(Suit.HEART, Value.VIII), Card(Suit.ACORN, Value.UNDER),381 Card(Suit.ACORN, Value.OVER), Card(Suit.ACORN, Value.KING), Card(Suit.ACORN, Value.ACE),382 Card(Suit.LEAF, Value.VII), Card(Suit.LEAF, Value.VIII), Card(Suit.LEAF, Value.X),383 Card(Suit.LEAF, Value.UNDER), Card(Suit.LEAF, Value.OVER), Card(Suit.BELL, Value.VIII),384 Card(Suit.BELL, Value.X), Card(Suit.BELL, Value.UNDER), Card(Suit.BELL, Value.OVER),385 Card(Suit.BELL, Value.KING), Card(Suit.HEART, Value.OVER),386 Card(Suit.ACORN, Value.VIII)],387 lp=(Hand([Card(Suit.HEART, Value.VII), Card(Suit.ACORN, Value.VII)]),388 Hand([Card(Suit.LEAF, Value.KING), Card(Suit.BELL, Value.VII)]),389 Hand([]),390 Hand([Card(Suit.HEART, Value.IX), Card(Suit.HEART, Value.X), Card(Suit.HEART, Value.UNDER),391 Card(Suit.BELL, Value.IX), Card(Suit.LEAF, Value.IX), Card(Suit.ACORN, Value.IX),392 Card(Suit.ACORN, Value.X)])),393 ace=3,394 suit=top.suit,395 val=top.value,396 cnt=0,397 i=3,398 mc=11,399 lp_mc=(-1, -1, 4, -1),400 deck_size=len(deck.cards)401 )402 legal_moves1: List[Move] = self.assert_and_make_legal_moves(1, state1, moves)403 next_mv1 = legal_moves1[0]404 state2 = next_mv1.apply(state1)405 self.assert_no_change(state1, state2, 'dp', 'st', 'suit', 'val', 'cnt', 'lp_mc')406 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)407 self.assert_change(408 state2,409 ace=state1.ace - 1,410 i=0,411 )412 legal_moves2: List[Move] = self.assert_and_make_legal_moves(1, state2, moves)413 next_mv2 = legal_moves2[0]414 state3 = next_mv2.apply(state2)415 self.assert_no_change(state2, state3, 'dp', 'st', 'suit', 'val', 'cnt', 'lp_mc')416 self.assert_change_in_lp_dp_mc(state2, state3, next_mv1)417 self.assert_change(418 state3,419 ace=state2.ace - 1,420 i=1,421 )422 legal_moves3: List[Move] = self.assert_and_make_legal_moves(1, state3, moves)423 next_mv3 = legal_moves3[0]424 state4 = next_mv3.apply(state3)425 self.assert_no_change(state3, state4, 'dp', 'st', 'suit', 'val', 'lp_mc')426 self.assert_change_in_lp_dp_mc(state3, state4, next_mv1)427 self.assert_change(428 state4,429 ace=state3.ace - 1,430 i=3,431 cnt=1432 )433 legal_moves4: List[Move] = self.assert_and_make_legal_moves(1, state4, moves)434 next_mv4 = legal_moves4[0]435 state5 = next_mv4.apply(state4)436 self.assert_no_change(state4, state5, 'dp', 'ace', 'suit', 'val', 'cnt', 'lp_mc')437 self.assert_change_in_lp_dp_mc(state4, state5, next_mv1)438 self.assert_change(439 state5,440 st=state4.st.delete(0, state4.cnt),441 i=0442 )443 self.assertTrue(state4.st[0] in state5.lp[state4.i])444 top = Card(Suit.LEAF, Value.VII)445 state1 = GameState(446 dp=[Card(Suit.HEART, Value.VII), Card(Suit.ACORN, Value.VII), top],447 st=[Card(Suit.HEART, Value.KING), Card(Suit.HEART, Value.VIII), Card(Suit.ACORN, Value.UNDER),448 Card(Suit.ACORN, Value.OVER), Card(Suit.ACORN, Value.KING), Card(Suit.ACORN, Value.ACE),449 Card(Suit.LEAF, Value.VIII), Card(Suit.LEAF, Value.X), Card(Suit.HEART, Value.ACE),450 Card(Suit.LEAF, Value.UNDER), Card(Suit.LEAF, Value.OVER), Card(Suit.BELL, Value.VIII),451 Card(Suit.BELL, Value.X), Card(Suit.BELL, Value.UNDER), Card(Suit.BELL, Value.OVER),452 Card(Suit.BELL, Value.KING), Card(Suit.HEART, Value.OVER),453 Card(Suit.ACORN, Value.VIII)],454 lp=(Hand([Card(Suit.LEAF, Value.ACE), Card(Suit.BELL, Value.ACE)]),455 Hand([Card(Suit.LEAF, Value.KING), Card(Suit.BELL, Value.VII)]),456 Hand([]),457 Hand([Card(Suit.HEART, Value.IX), Card(Suit.HEART, Value.X), Card(Suit.HEART, Value.UNDER),458 Card(Suit.BELL, Value.IX), Card(Suit.LEAF, Value.IX), Card(Suit.ACORN, Value.IX),459 Card(Suit.ACORN, Value.X)])),460 ace=0,461 suit=top.suit,462 val=top.value,463 cnt=9,464 i=3,465 mc=11,466 lp_mc=(-1, -1, 4, -1),467 deck_size=len(deck.cards)468 )469 legal_moves1: List[Move] = self.assert_and_make_legal_moves(1, state1, moves)470 next_mv1 = legal_moves1[0]471 state2 = next_mv1.apply(state1)472 self.assert_no_change(state1, state2, 'dp', 'ace', 'suit', 'val', 'lp_mc')473 self.assert_change_in_lp_dp_mc(state1, state2, next_mv1)474 self.assert_change(475 state2,476 st=state1.st.delete(0, state1.cnt),477 cnt=1,478 i=0,479 )480 self.assertTrue(all(c in state2.lp[state1.i] for c in state1.st[0:state1.cnt]))481if __name__ == '__main__':...

Full Screen

Full Screen

test_caching_allocator.py

Source:test_caching_allocator.py Github

copy

Full Screen

...30 def allocate(*size):31 ct.set_device(device)32 t = torch.Tensor(*size).float().to(ct.mlu_device())33 return t+034 def assert_change(comp=1, empty_cache=False):35 # comp > 0: increased36 # comp = 0: equal37 # comp < 0: decreased38 new_m = ct.memory_allocated(device)39 new_max_m = ct.max_memory_allocated(device)40 if comp > 0:41 self.assertGreater(new_m, last_m_arr[0])42 elif comp < 0:43 self.assertLess(new_m, last_m_arr[0])44 else:45 self.assertEqual(new_m, last_m_arr[0])46 self.assertLessEqual(new_m, new_max_m)47 self.assertGreaterEqual(new_max_m, max_m_arr[0])48 last_m_arr[0] = new_m49 max_m_arr[0] = new_max_m50 new_c = ct.memory_cached(device)51 new_max_c = ct.max_memory_cached(device)52 self.assertLessEqual(new_c, new_max_c)53 self.assertGreaterEqual(new_max_c, max_c_arr[0])54 last_c_arr[0] = new_c55 max_c_arr[0] = new_max_c56 if empty_cache:57 ct.empty_cached_memory()58 new_c = ct.memory_cached(device)59 new_max_c = ct.max_memory_cached(device)60 self.assertLessEqual(new_c, last_c_arr[0])61 self.assertLessEqual(new_c, new_max_c)62 self.assertEqual(new_max_c, max_c_arr[0])63 last_c_arr[0] = new_c64 assert_change(0)65 assert_change(0, empty_cache=True)66 assert_change(0)67 yield68 tensors1 = [allocate(1), allocate(10, 20), allocate(200, 300, 2000)]69 #check the memory status of empty device70 device_count = ct.device_count()71 for i in range(device_count):72 if i==device:73 self.assertEqual(ct.memory_cached(i), ct.memory_cached())74 self.assertEqual(ct.max_memory_cached(i), ct.max_memory_cached())75 self.assertEqual(ct.memory_allocated(i), ct.memory_allocated())76 self.assertEqual(ct.max_memory_allocated(i), ct.max_memory_allocated())77 continue78 self.assertEqual(ct.memory_cached(i), 0)79 self.assertEqual(ct.max_memory_cached(i), 0)80 self.assertEqual(ct.memory_allocated(i), 0)81 self.assertEqual(ct.max_memory_allocated(i), 0)82 m1 = ct.memory_allocated(device)83 assert_change(1)84 yield85 tensors2 = []86 # small chunks with allocation smaller than 1MB87 for i in range(1, int(N / 2) + 1):88 # small ones89 tensors2.append(allocate(i, i * 4))90 assert_change(1)91 yield92 # large chunks with allocation larger than 1MB93 for i in range(5, int(N / 2) + 5):94 tensors2.append(allocate(i, i * 7, i * 9, i * 11))95 assert_change(1)96 yield97 # TODO: unsupported empty tensor calculation98 # tensors2.append(allocate(0, 0, 0))99 # assert_change(0)100 # yield101 permute = []102 for i in torch.randperm(len(tensors2)):103 permute.append(tensors2[i])104 assert_change(0)105 yield106 del tensors2107 # now the memory of tensor2 is used by permute108 assert_change(0)109 yield110 tensors2 = permute111 assert_change(0)112 yield113 del permute114 # now the memory of permute is used by tensor2115 assert_change(0)116 yield117 for i in range(int(N / 2)):118 x = tensors2[i].numel()119 del tensors2[i]120 assert_change(-x) # in case that tensors2[i] is empty121 yield122 for i in range(2, int(2 * N / 3) + 2):123 tensors2.append(allocate(i, i * 3, i * 8))124 assert_change(1)125 yield126 del tensors2127 assert_change(-1)128 assert_change(0)129 self.assertEqual(ct.memory_allocated(device), m1)130 yield True131 del tensors1132 assert_change(-1)133 self.assertEqual(ct.memory_allocated(device), m0)134 if int(os.environ.get("ENABLE_CATCH_MEMORY_DEBUG")) :135 t3 = allocate(100)136 ct.memory_debug(t3)137 ct.memory_debug()138 assert_change(1)139 # test empty_cache140 assert_change(0, empty_cache=True)141 #@unittest.skip("not test")142 @testinfo()143 def test_memory_stats(self):144 os.environ["ENABLE_CATCH_MEMORY_DEBUG"] = '0'145 ct.empty_cached_memory()146 for _ in self._test_memory_generator(self):147 pass148 os.environ["ENABLE_CATCH_MEMORY_DEBUG"] = '1'149 for _ in self._test_memory_generator(self):150 pass151 os.environ["ENABLE_CATCH_MEMORY_DEBUG"] = '0'152if __name__ == '__main__':...

Full Screen

Full Screen

test_release_usecase.py

Source:test_release_usecase.py Github

copy

Full Screen

...4 Release,5 ReleaseComparingResult,6 ReleaseUsecase,7)8def assert_change(9 release: Release, db_release: ProductReleaseInfoInDB, status: ReleaseComparingResult10):11 assert status in ReleaseUsecase.get_release_comparing_results(release, db_release)12def test_ignore_case():13 release = Release(14 release_date=date(2222, 2, 1),15 )16 db_release = ProductReleaseInfoInDB(17 id=2,18 product_id=1,19 created_at=datetime.now(),20 updated_at=datetime.now(),21 price=100,22 tax_including=False,23 initial_release_date=date(1111, 1, 1),24 adjusted_release_date=date(2222, 2, 1),25 shipped_at=date(2222, 2, 27),26 )27 assert (28 ReleaseComparingResult.IGNORE29 in ReleaseUsecase.get_release_comparing_results(release, db_release)30 )31 db_release.shipped_at = None32 assert (33 ReleaseComparingResult.IGNORE34 in ReleaseUsecase.get_release_comparing_results(release, db_release)35 )36 release.release_date = date(1999, 9, 9)37 assert (38 ReleaseComparingResult.IGNORE39 in ReleaseUsecase.get_release_comparing_results(release, db_release)40 )41def test_date_change_case():42 release = Release(43 release_date=date(2222, 2, 1),44 )45 db_release = ProductReleaseInfoInDB(46 id=1,47 product_id=1,48 created_at=datetime.now(),49 updated_at=datetime.now(),50 price=100,51 tax_including=False,52 initial_release_date=date(1111, 1, 1),53 adjusted_release_date=date(2222, 2, 2),54 )55 assert_change(release, db_release, ReleaseComparingResult.DATE_CHANGE)56 db_release.initial_release_date = None57 db_release.adjusted_release_date = None58 assert_change(release, db_release, ReleaseComparingResult.DATE_CHANGE)59 db_release.initial_release_date = date(111, 1, 1)60 db_release.adjusted_release_date = None61 assert_change(release, db_release, ReleaseComparingResult.DATE_CHANGE)62def test_price_change_case():63 release = Release(64 price=200,65 tax_including=True,66 release_date=date(2222, 2, 1),67 )68 db_release = ProductReleaseInfoInDB(69 id=3,70 product_id=1,71 created_at=datetime.now(),72 updated_at=datetime.now(),73 price=100,74 tax_including=False,75 initial_release_date=date(1111, 1, 1),76 adjusted_release_date=date(2222, 2, 2),77 )78 assert_change(release, db_release, ReleaseComparingResult.PRICE_CHANGE)79def test_build_patch_data(release_factory):80 release = release_factory.build()81 release_patch = ReleaseUsecase.build_release_patch_data_by_status(82 incoming_release=release, status_indicator=[ReleaseComparingResult.PRICE_CHANGE]83 )84 assert release.price == release_patch.price85 assert release.tax_including == release_patch.tax_including86 release_patch = ReleaseUsecase.build_release_patch_data_by_status(87 incoming_release=release, status_indicator=[ReleaseComparingResult.DATE_CHANGE]88 )...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful