From f2334f0c99301f31a0078c17c83764e3a452ea5f Mon Sep 17 00:00:00 2001 From: veeti Date: Wed, 19 Sep 2018 20:30:19 +0300 Subject: [PATCH 1/2] tests for tennis game --- bin/TennisGame.class | Bin 1823 -> 1823 bytes bin/TennisGameTest.class | Bin 1393 -> 4186 bytes src/TennisGame.java | 180 ++++++++++----------- tests/TennisGameTest.java | 321 ++++++++++++++++++++++++++++++-------- 4 files changed, 344 insertions(+), 157 deletions(-) diff --git a/bin/TennisGame.class b/bin/TennisGame.class index 2da4e7a578a8ea5a5a076c2766b0eebecc4efc75..72450977ece520910a5bc96d3befd388de29f454 100644 GIT binary patch delta 31 ncmbQwH=l1qJ{#l0$pvg;yxKb$Y_~Hw3%PA$@X(n&nXLx^oInYp delta 31 ncmbQwH=l1qJ{#kb$pvg;ygEA=Y_~Hw3%PA$@X($-nXLx^oRJBo diff --git a/bin/TennisGameTest.class b/bin/TennisGameTest.class index b202db2907805d8b5bb4b6f773ec5588f796fb7a..8d53affeb6752d8ac1f4ea814f876761f801a6f7 100644 GIT binary patch literal 4186 zcmbuBYitx%6vxk$!cLb(V4#Io9!phdOWS3)T@;pwr4%SGuWl)#NSW?lcBgJ<%gk(r z_z1x-MvWR{3}1{GHO3fYFi{jD#u#H_j2bn@7-RH{@sa2k<0Dby|IW1aswn#LqrFBT6+TL7>`Dqr_1!y+Sk!XHg)7jqq*a$NQ)sZZdNQvi?YIaC9 zG(L7;)>;|OgoAjg*O)|$EqE`!9Zp!P0l$p{dEL^+*pOywFw>*!IZL&)oDM7P@zKAgz)ppxUFp6L~cYF&kPYyA<9yd5BGZV`n3c?7^ZCRYY{Q&Bzlm zNsC0GvT*mQndJVgdWso`G`)L&4$(A+?QjflXZa+o`}~9m9Tt1uH z#^T69IC?y1IHDpl-H1roOM;qdrwEw}M`hX|QJtH(a1=!~oz-xZ+x4u5W!fZK4M*MV z%Y>a$=M5!VGo970b>XOk34=X|p@-SE&V}ri$mb5K80!{|?GTN17mV$KF?S%*$}%z+ z#EBznL8e=?sc~4~lNw$*DaxmAQyuZog+PQf_X*Gv`c6)H)sA zE_H(Sq$X6|QqxSPgP!4VFr%75wK#PMQw7eFDTz9!^8o!l$I(^jb+l}{5E%*8d}EWmvpMmQHSTLUxBss;8sqGK@69W-2` zg@-TDqWId^Y4ILqsnXQ`1}!UER;U)MnnRD8u7kIER7cm- z4e%(@YIvSSYw&$7MlBvgo6B&{WmqpTtOZBm*NNxCkH6#3U<@l=rHm0!5WLFp7=XV8 z@U@uQXeq4)bvsU}PRxezHjMV&MN#}`;E{4PtoJl@x*9qJ4bdtZGOmUuL4(V=Ub(51 zvjK@D+5pZNd~U|6v4t9GtH9Fau_!JJ&UP=J8?SYFY;Mf@l%1t~%^)K>g6mQE9|qqs@TsV?B>p6&Q1wh;nPEW{Bp+^xhbF^ws)2f_OgcpnDu zBj9}${pB(Ak;kz|pP*KH5*_BOz|K39Qv+>o4YUe1aBCIz*If4AD`qc>Ib2>K8_}wX zo`RUC0e22!o`INWA?7)Vc^+b3fS4B{<|T-E8O`z)+DPYV8@(zB>-B`iJYh{a)*2Vu{XgIiQa9=(}$=R)^`0v2!*x!Tu2XOxg?w`Q@Gq`^N_pdfv wgJaMun0mKhBszYzMB=8kpUa7U1JUpB_y>so1kqn0`Wr<5R3S=3dJSd%1HIoo9smFU delta 129 zcmcbm@R5t_)W2Q(7#J8#7_2sO?Pp@Nn=HU0Hd%+wck(Va9Y&VT&)Ft1@^CXSGH3y@ zSu%t9h| aKzUvu&A}kbpv0gIG=`DE0?21#umk|29u=4 && player1Points-player2Points>=2) - gameEnded = true; - else if (player2Points>=4 && player2Points-player1Points>=2) - gameEnded = true; - } - - private String getScore(int points) { - switch (points) { - case 0: return "love"; - case 1: return "15" ; - case 2: return "30" ; - case 3: return "40"; - default: return "40" ; - } - } - - public void player1Scored() throws TennisGameException { - if (gameEnded) { - throw new TennisGameException(); - } - else { - player1Points++; - checkGameEnded(); - } - } - - public void player2Scored() throws TennisGameException { - if (gameEnded) { - throw new TennisGameException(); - } - else { - player2Points++; - checkGameEnded(); - } - } - - public String getScore() { -// Here is the format of the scores: -// "love - love" -// "15 - 15" -// "30 - 30" -// "deuce" -// "15 - love", "love - 15" -// "30 - love", "love - 30" -// "40 - love", "love - 40" -// "30 - 15", "15 - 30" -// "40 - 15", "15 - 40" -// "player1 has advantage" -// "player2 has advantage" -// "player1 wins" -// "player2 wins" - - String player1Score = getScore(player1Points); - String player2Score = getScore(player2Points); - - if (gameEnded) { - if (player1Points > player2Points) - return "player1 wins"; - else - return "player2 wins"; - } - - if (player1Points >= 4 && player1Points == player2Points) - return "deuce"; - - if (player1Points >= 4 && player1Points - player2Points == 1) - return "player1 has advantage"; - - if (player2Points > 4 && player2Points - player1Points == 1) - return "player2 has advantage"; - - return player2Score + " - " + player1Score ; - } +// This implementation is used for practicing unit tests. +// NOTE THAT it may contain bugs +// Write unit tests in TennisGameTest.java and try to find the errors in the code + +public class TennisGame { + private int player1Points; + private int player2Points; + + private boolean gameEnded; + + public TennisGame() { + player1Points = 0; + player2Points = 0; + gameEnded = false ; + } + + private void checkGameEnded() { + if (player1Points>=4 && player1Points-player2Points>=2) + gameEnded = true; + else if (player2Points>=4 && player2Points-player1Points>=2) + gameEnded = true; + } + + private String getScore(int points) { + switch (points) { + case 0: return "love"; + case 1: return "15" ; + case 2: return "30" ; + case 3: return "40"; + default: return "40" ; + } + } + + public void player1Scored() throws TennisGameException { + if (gameEnded) { + throw new TennisGameException(); + } + else { + player1Points++; + checkGameEnded(); + } + } + + public void player2Scored() throws TennisGameException { + if (gameEnded) { + throw new TennisGameException(); + } + else { + player2Points++; + checkGameEnded(); + } + } + + public String getScore() { +// Here is the format of the scores: +// "love - love" +// "15 - 15" +// "30 - 30" +// "deuce" +// "15 - love", "love - 15" +// "30 - love", "love - 30" +// "40 - love", "love - 40" +// "30 - 15", "15 - 30" +// "40 - 15", "15 - 40" +// "player1 has advantage" +// "player2 has advantage" +// "player1 wins" +// "player2 wins" + + String player1Score = getScore(player1Points); + String player2Score = getScore(player2Points); + + if (gameEnded) { + if (player1Points > player2Points) + return "player1 wins"; + else + return "player2 wins"; + } + + if (player1Points >= 4 && player1Points == player2Points) + return "deuce"; + + if (player1Points >= 4 && player1Points - player2Points == 1) + return "player1 has advantage"; + + if (player2Points >= 4 && player2Points - player1Points == 1) + return "player2 has advantage"; + + return player1Score + " - " + player2Score ; + } } \ No newline at end of file diff --git a/tests/TennisGameTest.java b/tests/TennisGameTest.java index 8674eba..74426f1 100644 --- a/tests/TennisGameTest.java +++ b/tests/TennisGameTest.java @@ -1,67 +1,254 @@ -import static org.junit.Assert.*; - -import org.junit.Test; - -import jdk.nashorn.internal.ir.annotations.Ignore; - -public class TennisGameTest { - -// Here is the format of the scores: "player1Score - player2Score" -// "love - love" -// "15 - 15" -// "30 - 30" -// "deuce" -// "15 - love", "love - 15" -// "30 - love", "love - 30" -// "40 - love", "love - 40" -// "30 - 15", "15 - 30" -// "40 - 15", "15 - 40" -// "player1 has advantage" -// "player2 has advantage" -// "player1 wins" -// "player2 wins" - @Ignore - public void testTennisGame_Start() { - //Arrange - TennisGame game = new TennisGame(); - //Act - String score = game.getScore() ; - // Assert - assertEquals("Initial score incorrect", "love - love", score); - } - - @Test - public void testTennisGame_EahcPlayerWin4Points_Score_Deuce() throws TennisGameException { - //Arrange - TennisGame game = new TennisGame(); - - game.player1Scored(); - game.player1Scored(); - game.player1Scored(); - - game.player2Scored(); - game.player2Scored(); - game.player2Scored(); - - game.player1Scored(); - game.player2Scored(); - //Act - String score = game.getScore() ; - // Assert - assertEquals("Tie score incorrect", "deuce", score); - } - - @Test (expected = TennisGameException.class) - public void testTennisGame_Player1WinsPointAfterGameEnded_ResultsException() throws TennisGameException { - //Arrange - TennisGame game = new TennisGame(); - //Act - game.player1Scored(); - game.player1Scored(); - game.player1Scored(); - game.player1Scored(); - //Act - // This statement should cause an exception - game.player1Scored(); - } -} +import static org.junit.Assert.*; + +import org.junit.Test; + +import jdk.nashorn.internal.ir.annotations.Ignore; + +public class TennisGameTest { + +// Here is the format of the scores: "player1Score - player2Score" +// "love - love" +// "15 - 15" +// "30 - 30" +// "deuce" +// "15 - love", "love - 15" +// "30 - love", "love - 30" +// "40 - love", "love - 40" +// "30 - 15", "15 - 30" +// "40 - 15", "15 - 40" +// "player1 has advantage" +// "player2 has advantage" +// "player1 wins" +// "player2 wins" + @Ignore + public void testTennisGame_Start() { + //Arrange + TennisGame game = new TennisGame(); + //Act + String score = game.getScore(); + // Assert + assertEquals("Initial score incorrect", "love - love", score); + } + + @Test + public void testTennisGame_EahcPlayerWin4Points_Score_Deuce() throws TennisGameException { + //Arrange + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + + game.player1Scored(); + game.player2Scored(); + //Act + String score = game.getScore(); + // Assert + assertEquals("Tie score incorrect", "deuce", score); + } + + @Test + public void testTennisGame_ScoreShouldBeLove15forPlayer2() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player2Scored(); + + String score = game.getScore(); + assertEquals("Score is love - 15", "love - 15", score); + } + + @Test + public void testTennisGame_ScoreShouldBe15LoveforPlayer1() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + + String score = game.getScore(); + assertEquals("Score is 15 - love", "15 - love", score); + } + + @Test + public void testTennisGame_ScoreShouldBe3015Player1() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + + String score = game.getScore(); + assertEquals("Score is 30 - 15", "30 - 15", score); + } + + @Test + public void testTennisGame_ScoreShouldBe4015Player1() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player2Scored(); + game.player1Scored(); + + String score = game.getScore(); + assertEquals("Score is 40 - 15", "40 - 15", score); + } + + @Test + public void testTennisGame_Player1ShouldWin() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + + String score = game.getScore(); + + assertEquals("Player1 wins staright", "player1 wins", score); + } + + @Test + public void testTennisGame_Player2ShouldWin() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + + String score = game.getScore(); + + assertEquals("Player2 wins staright", "player2 wins", score); + } + + @Test + public void testTennisGame_Player1ShouldHaveAdvantage() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + + game.player1Scored(); + + String score = game.getScore(); + + assertEquals("Player1 has advantage", "player1 has advantage", score); + + } + + @Test + public void testTennisGame_Player1ShouldWinAfterAdvantage() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + + game.player1Scored(); + + String score = game.getScore(); + + assertEquals("Player1 has advantage", "player1 has advantage", score); + + game.player1Scored(); + + score = game.getScore(); + + assertEquals("Player1 wins after advantage", "player1 wins", score); + } + + @Test + public void testTennisGame_Player2ShouldHaveAdvantage() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + + game.player2Scored(); + + String score = game.getScore(); + + assertEquals("Player2 has advantage", "player2 has advantage", score); + + } + + @Test + public void testTennisGame_Player2ShouldWinAfterAdvantage() throws TennisGameException { + + TennisGame game = new TennisGame(); + + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + + game.player2Scored(); + + String score = game.getScore(); + + assertEquals("Player2 has advantage", "player2 has advantage", score); + + game.player2Scored(); + + score = game.getScore(); + + assertEquals("Player2 wins after advantage", "player2 wins", score); + } + + @Test (expected = TennisGameException.class) + public void testTennisGame_Player1WinsPointAfterGameEnded_ResultsException() throws TennisGameException { + //Arrange + TennisGame game = new TennisGame(); + //Act + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + game.player1Scored(); + //Act + // This statement should cause an exception + game.player1Scored(); + } + + @Test (expected = TennisGameException.class) + public void testTennisGame_Player2WinsPointAfterGameEnded_ResultsException() throws TennisGameException { + //Arrange + TennisGame game = new TennisGame(); + //Act + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + game.player2Scored(); + //Act + // This statement should cause an exception + game.player2Scored(); + } +} From f7f3f768b95ff9fbe844cadb5a5471b371b3e6af Mon Sep 17 00:00:00 2001 From: veeti Date: Thu, 20 Sep 2018 20:29:59 +0300 Subject: [PATCH 2/2] Assignment Finished --- bin/TennisGameTest.class | Bin 4186 -> 4186 bytes tests/TennisGameTest.java | 55 -------------------------------------- 2 files changed, 55 deletions(-) diff --git a/bin/TennisGameTest.class b/bin/TennisGameTest.class index 8d53affeb6752d8ac1f4ea814f876761f801a6f7..eb73d41a8d1833714075bc217d8cb37e3f9e6aa3 100644 GIT binary patch delta 521 zcmW-dNl#N@7=_QWP@ptn;Z3+n4OqCEQb@d@L;*=;a)Au8C|YD8gSuc)1{+jlD9Qr# z1AzsI1x4N9KmwaaO9J6HH2esE0#Df8obx<6_x;j=bl|7Y6Dp8Lmz1TTa#f@}Il7?B z%GVVI6jGr|RID&xrE*nfZg^sT7o!}bLVjJ7rR(N|^d@N)X*FpL=`B*nOk_l?+cb4F z^(-{7&`8^)i@Ia}XT5CYGACk(LZifA zM3?X#kq}=lQnErs>JTLwA!A5hL<_8gWI}=uUBA!s@ZtB%_%r@_uPYRgOF?;5B%g|v zuM(Y8sm|+y{JN-s!dx#Yq%uVnF;%W)UO7c2#bxE`iY!%|k6dfG)^e@mTFB}-3pCE+n_h0?d;!@t=lTp9g}rCCVeJu-^IFz)q!;% z>j72*s}rm1zt!%J+uc}6tRAcsRxef`RzKE5EaN=J7{D0Bc!Kd1;~B3V;W{hP{QI!OpU?&CYjrcGz)D*c-EU lsrRV&see%aq&}ejWyZbT)*