File tree Expand file tree Collapse file tree 3 files changed +48
-19
lines changed Expand file tree Collapse file tree 3 files changed +48
-19
lines changed Original file line number Diff line number Diff line change @@ -336,7 +336,7 @@ public Block CreateBlock()
336336 /// <returns></returns>
337337 public Block CreateBlock ( int numberOfTrials )
338338 {
339- if ( numberOfTrials > 0 )
339+ if ( numberOfTrials >= 0 )
340340 return new Block ( ( uint ) numberOfTrials , this ) ;
341341 else
342342 throw new Exception ( "Invalid number of trials supplied" ) ;
@@ -468,24 +468,21 @@ Trial GetFirstTrial()
468468 /// <returns></returns>
469469 Trial GetLastTrial ( )
470470 {
471- Block lastBlock ;
472- try
473- {
474- lastBlock = blocks [ blocks . Count - 1 ] ;
475- }
476- catch ( ArgumentOutOfRangeException )
477- {
478- throw new NoSuchTrialException ( "There is no last trial because no blocks have been created!" ) ;
479- }
471+ if ( blocks . Count == 0 ) throw new NoSuchTrialException ( "There is no last trial because no blocks have been created!" ) ;
480472
481- try
473+ Block lastValidBlock ;
474+ int i = blocks . Count - 1 ;
475+ while ( i >= 0 )
482476 {
483- return lastBlock . trials [ lastBlock . trials . Count - 1 ] ;
484- }
485- catch ( ArgumentOutOfRangeException )
486- {
487- throw new NoSuchTrialException ( "There is no last trial. No trials exist in the last block." ) ;
477+ lastValidBlock = blocks [ i ] ;
478+ if ( lastValidBlock . trials . Count > 0 )
479+ {
480+ return lastValidBlock . trials [ lastValidBlock . trials . Count - 1 ] ;
481+ }
482+ i -- ;
488483 }
484+
485+ throw new NoSuchTrialException ( "There is no last trial, blocks are present but they are all empty." ) ;
489486 }
490487
491488 /// <summary>
Original file line number Diff line number Diff line change @@ -160,14 +160,46 @@ public void FirstLast()
160160 Block block1 = session . CreateBlock ( 10 ) ;
161161 Block block2 = session . CreateBlock ( 10 ) ;
162162
163- Assert . AreEqual ( block1 . trials [ 0 ] , session . FirstTrial ) ;
164- Assert . AreEqual ( block2 . trials [ 9 ] , session . LastTrial ) ;
163+ Assert . AreEqual ( block1 . GetRelativeTrial ( 1 ) , session . FirstTrial ) ;
164+ Assert . AreEqual ( block2 . GetRelativeTrial ( 10 ) , session . LastTrial ) ;
165+
166+ // reset blocks
167+ session . blocks = new List < Block > ( ) ;
168+ }
169+
170+
171+ [ Test ]
172+ public void GetLastTrialWhenLastBlockEmpty ( )
173+ {
174+ Block block1 = session . CreateBlock ( 10 ) ;
175+ Block block2 = session . CreateBlock ( ) ;
176+
177+ Assert . AreEqual ( session . LastTrial , block1 . GetRelativeTrial ( 10 ) ) ;
165178
166179 // reset blocks
167180 session . blocks = new List < Block > ( ) ;
168181 }
169182
183+ [ Test ]
184+ public void GetLastTrialWhenNoTrials ( )
185+ {
186+ Block block1 = session . CreateBlock ( ) ;
187+ Block block2 = session . CreateBlock ( ) ;
188+
189+ Assert . Throws < NoSuchTrialException > ( ( ) => { var a = session . LastTrial ; } ) ;
190+
191+ // reset blocks
192+ session . blocks = new List < Block > ( ) ;
193+ }
170194
195+ [ Test ]
196+ public void GetLastTrialWhenNoBlocks ( )
197+ {
198+ Assert . Throws < NoSuchTrialException > ( ( ) => { var a = session . LastTrial ; } ) ;
199+
200+ // reset blocks
201+ session . blocks = new List < Block > ( ) ;
202+ }
171203
172204 }
173205
Original file line number Diff line number Diff line change 1- 2.2.3
1+ 2.2.4
You can’t perform that action at this time.
0 commit comments