From 3a3ed278453a361b22f1a56192ddbc79c4f21919 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 1 Mar 2019 19:48:05 -0500 Subject: [PATCH] Added a Stopwatch class and tests to make sure it works propperly --- mattrixwv/Stopwatch$1.class | Bin 0 -> 969 bytes mattrixwv/Stopwatch$TIME_RESOLUTION.class | Bin 0 -> 1281 bytes mattrixwv/Stopwatch.class | Bin 0 -> 3039 bytes mattrixwv/Stopwatch.java | 121 ++++++++++++++++++++++ testStopwatch.java | 67 ++++++++++++ 5 files changed, 188 insertions(+) create mode 100644 mattrixwv/Stopwatch$1.class create mode 100644 mattrixwv/Stopwatch$TIME_RESOLUTION.class create mode 100644 mattrixwv/Stopwatch.class create mode 100644 mattrixwv/Stopwatch.java create mode 100644 testStopwatch.java diff --git a/mattrixwv/Stopwatch$1.class b/mattrixwv/Stopwatch$1.class new file mode 100644 index 0000000000000000000000000000000000000000..03b25becc52699a4a7c330065ccf117d163290b7 GIT binary patch literal 969 zcma)*ZEw<06vzLUrvmGsI;V(FQ_2J%z^82{8soyctPxs6L0{~JmeP#Qt|e^&zmc!i z#291t0sJ(60^_-;U@~5;Y5wQ@&b{ZH+k0<+{Q7nbU<;44DB@BEA}(jhyTb9RfNLDq zxLoIQgG-5v%%v<~T|kAQCL0qcvIZ@4C?A);&2C54 zHHO%}%8-6)xsK;V&luvB>MMps!|&S+`KII9+UTff2OYELQe@ePOzRz0|I1m!9|e}Z z>+r$ctWWLG9GjG{c$Vvjj(5%_S4(gh3jI6<~W4nDN2>d|A13v8`mq%PSxNLISLY-lC_R;m(Vq_{Qs-9;D4c81q zJ7kdN9rWIzZAA<#3o;o>3!*1CF@IuPQ)AN|@lKUWb-#IvB2O5y+P0<}N<-In7|N$i z68xa=c&0l&IXcRvmfC2~st`4s>c5()nbT6WZbzYW-qX8nhNRMN>uqU`F5NO+LM9dX zk#G-bNH|0GEYVNI$pc1T!H&OYKO^>uzws|fB=iqRyqIE&FgZtoFg3?Cp)kh`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fPZSW@uFy4;f9To55y$CKu@O}gzMDSq*7X>=TXLMVS zEox3oGh@1KTXrmK73St$CLeQW^qA`%d}Bd#S&-)!7kZ1i3pB5xjqeoLx|Y7Iu}X3p zw&uOgC{g+KXRTK=mmSA*U(oXN`q){4#>AmD+NU(elH=+H_Jhej@q3i5d5ERWGt`8P z!?VkIn+JEziQ&?iJ~6g=G`%-HmKhluOS9EFl6R3HofON?8K%au;Bd>FU!uySF7VT1q_7lSKHeWs|PJqluDP3)*|CMCCWup3*3R zl|WBCvuLp2IhFBZ?(t$P#LEu1Vrn#sOZZ4&gNVuUJKhj72!N_hrO{;(a?%v@e~;=bjclHvxHEYaNNIKDKe zuVMZ_#}kAY((?I?;p%)?eaYr5OK2@_0&VNtp`CBBHoizI_|J!#hxk0>Rppz7&p65l zdDqW%!E0UDPY_+=M^s2r`8`zv4^hfD50@!7qMCQLXrv1tfI)z1uE+onb^U;f>xfjZ zAasSe@;Vw;P$jj?2$|uGLzmO8r3J8!ujK^EchY)y(R(=X-c6eD&ARjgU7f4y8Nen#8zO8VWuf>`EwXh(on(7ro#6T*;VSDEb zwmCSU;2eQ<0p!==3I5LVXKxN&!&YE-=rV>Rj{A5(;tn5oOT5j;_e=bMj~gXU_*iM^ z+#T8+l3%HQJ5QjQcpNpDr2R2=?<9|L3Q0W6yOYR5gGu=UayX9}d= 1000);resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1]){ + duration /= 1000; + } + //Check if the duration needs reduced to minutes + if(duration >= 1000){ + //Reduce to minutes + duration /= 60; + resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1]; + } + //Check if the duration needs reduced to hours + if(duration >= 1000){ + //Reduce to hours + duration /= 60; + resolution = TIME_RESOLUTION.values()[resolution.ordinal() + 1]; + } + + //Turn the number into a string + Double durationFraction = ((duration % 1) * 100); + String time = String.format("% 3d.%03d", duration.intValue(), durationFraction.intValue()); + + //Tack on the appropriate suffix for resolution + switch(resolution){ + case NANOSECOND: time += " nanoseconds"; break; + case MICROSECOND: time += " microseconds"; break; + case MILLISECOND: time += " milliseconds"; break; + case SECOND: time += " seconds"; break; + case MINUTE: time += " minutes"; break; + case HOUR: time += " hours"; break; + case ERROR: + default: time = "There was an error computing the time"; break; ///This should throw an error instead + } + //Return the string + return time; + } +} diff --git a/testStopwatch.java b/testStopwatch.java new file mode 100644 index 0000000..d715cf1 --- /dev/null +++ b/testStopwatch.java @@ -0,0 +1,67 @@ +//Java/JavaClasses/testStopwatch.java +//Matthew Ellison +// Created: 03-01-19 +//Modified: 03-01-19 +//This class is used to test that stopwatch does what it is supposed to do (approximately) + + +import mattrixwv.Stopwatch; + + +public class testStopwatch{ + private static final int NUM_TO_RUN = 100000; + public static void main(String[] argv){ + Boolean failed = false; //A flag to determine if all tests were passed + //Print the test begin message + System.out.println("Begin test"); + //Describe which test is being performed + System.out.println("Starting loop"); + //Start the timer + Stopwatch timer = new Stopwatch(); + timer.start(); + for(int cnt = 0;cnt < NUM_TO_RUN;++cnt){ + System.out.print(cnt); + } + //Stop the timer + timer.stop(); + System.out.println("\nLoop completed"); + + //Check that the different resolutions work out correctly + System.out.println("Checking that resolutions line up correctly"); + Double nano = timer.getNano(); + if(timer.getMicro() != (nano.doubleValue() / 1000D)){ + System.out.println("Error on microsecond resolution"); + failed = true; + } + else if(timer.getMilli() != (nano.doubleValue() / 1000000D)){ + System.out.println("Error on millisecond resolution"); + failed = true; + } + else if(timer.getSecond() != (nano.doubleValue() / 1000000000D)){ + System.out.println("Error on second resolution"); + failed = true; + } + else if(timer.getMinute() != (nano.doubleValue() / 60000000000D)){ + System.out.println("Error on minute resolution"); + failed = true; + } + else if(timer.getHour() != (nano.doubleValue() / 3600000000000D)){ + System.out.println("Error on hour resolution"); + failed = true; + } + else{ + System.out.println("All time resolution tests completed successfully"); + } + System.out.println("Completed resolution checking"); + + //Print the results + System.out.printf("The timer results in: %s\n", timer.getStr()); + + if(!failed){ + System.out.println("All tests completed successfully"); + } + else{ + System.out.println("Test failed. Try again!"); + } + } +} \ No newline at end of file