From ff253c31dda8ad7583dd13ce6796dc7d303fe9ec Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 16 Oct 2017 14:22:38 +0300 Subject: [PATCH 1/5] codestyle format --- kek2/Java | 1 + 1 file changed, 1 insertion(+) create mode 160000 kek2/Java diff --git a/kek2/Java b/kek2/Java new file mode 160000 index 0000000..d207fbc --- /dev/null +++ b/kek2/Java @@ -0,0 +1 @@ +Subproject commit d207fbc8f6316ee94011c17d43e488e48f2e61eb From 66865afcebe7d64f67ad9e3b530bbcdceee4545f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Mon, 16 Oct 2017 14:23:18 +0300 Subject: [PATCH 2/5] codestyle format --- Untitled Folder 2/Java/.gitignore | 49 +++++++++++ Untitled Folder 2/Java/.idea/compiler.xml | 16 ++++ Untitled Folder 2/Java/.idea/misc.xml | 13 +++ Untitled Folder 2/Java/.idea/modules.xml | 8 ++ Untitled Folder 2/Java/Maybe.iml | 17 ++++ Untitled Folder 2/Java/pom.xml | 36 ++++++++ .../java/ru/spbau/mit/kazakov/Maybe/Main.java | 63 +++++++++++++ .../ru/spbau/mit/kazakov/Maybe/Maybe.java | 83 ++++++++++++++++++ .../kazakov/Maybe/getNothingException.java | 7 ++ .../ru/spbau/mit/kazakov/Maybe/MainTest.java | 47 ++++++++++ .../ru/spbau/mit/kazakov/Maybe/MaybeTest.java | 60 +++++++++++++ .../ru/spbau/mit/kazakov/Maybe/Main.class | Bin 0 -> 3978 bytes .../ru/spbau/mit/kazakov/Maybe/Maybe.class | Bin 0 -> 2100 bytes .../kazakov/Maybe/getNothingException.class | Bin 0 -> 339 bytes .../compile/default-compile/createdFiles.lst | 3 + .../compile/default-compile/inputFiles.lst | 3 + .../default-testCompile/createdFiles.lst | 2 + .../default-testCompile/inputFiles.lst | 2 + ...ST-ru.spbau.mit.kazakov.Maybe.MainTest.xml | 64 ++++++++++++++ ...T-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml | 70 +++++++++++++++ .../ru.spbau.mit.kazakov.Maybe.MainTest.txt | 4 + .../ru.spbau.mit.kazakov.Maybe.MaybeTest.txt | 4 + .../ru/spbau/mit/kazakov/Maybe/MainTest.class | Bin 0 -> 2225 bytes .../spbau/mit/kazakov/Maybe/MaybeTest.class | Bin 0 -> 3912 bytes 24 files changed, 551 insertions(+) create mode 100644 Untitled Folder 2/Java/.gitignore create mode 100644 Untitled Folder 2/Java/.idea/compiler.xml create mode 100644 Untitled Folder 2/Java/.idea/misc.xml create mode 100644 Untitled Folder 2/Java/.idea/modules.xml create mode 100644 Untitled Folder 2/Java/Maybe.iml create mode 100644 Untitled Folder 2/Java/pom.xml create mode 100644 Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java create mode 100644 Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java create mode 100644 Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java create mode 100644 Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java create mode 100644 Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java create mode 100644 Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Main.class create mode 100644 Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Maybe.class create mode 100644 Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/getNothingException.class create mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst create mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst create mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst create mode 100644 Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MainTest.xml create mode 100644 Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml create mode 100644 Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt create mode 100644 Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt create mode 100644 Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MainTest.class create mode 100644 Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MaybeTest.class diff --git a/Untitled Folder 2/Java/.gitignore b/Untitled Folder 2/Java/.gitignore new file mode 100644 index 0000000..345e61a --- /dev/null +++ b/Untitled Folder 2/Java/.gitignore @@ -0,0 +1,49 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/Untitled Folder 2/Java/.idea/compiler.xml b/Untitled Folder 2/Java/.idea/compiler.xml new file mode 100644 index 0000000..067d5ff --- /dev/null +++ b/Untitled Folder 2/Java/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Untitled Folder 2/Java/.idea/misc.xml b/Untitled Folder 2/Java/.idea/misc.xml new file mode 100644 index 0000000..e8942bd --- /dev/null +++ b/Untitled Folder 2/Java/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/Untitled Folder 2/Java/.idea/modules.xml b/Untitled Folder 2/Java/.idea/modules.xml new file mode 100644 index 0000000..65c89bb --- /dev/null +++ b/Untitled Folder 2/Java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Untitled Folder 2/Java/Maybe.iml b/Untitled Folder 2/Java/Maybe.iml new file mode 100644 index 0000000..57f498f --- /dev/null +++ b/Untitled Folder 2/Java/Maybe.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Untitled Folder 2/Java/pom.xml b/Untitled Folder 2/Java/pom.xml new file mode 100644 index 0000000..fe36c91 --- /dev/null +++ b/Untitled Folder 2/Java/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + ru.spbau.mit.kazakov.Maybe + Maybe + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + junit + junit + 4.8 + + + org.jetbrains + annotations + 13.0 + + + + \ No newline at end of file diff --git a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java new file mode 100644 index 0000000..8bd1447 --- /dev/null +++ b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java @@ -0,0 +1,63 @@ +package ru.spbau.mit.kazakov.Maybe; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.*; +import java.util.ArrayList; +import java.util.Scanner; + +/** + * Reads numbers from input file and writes squared values to output file. If read data isn't a number writes "null" instead. + */ +public class Main { + /** + * Primary method. + * + * @param args the first argument is input file and the second one is output file + */ + public static void main(@NotNull String[] args) throws IOException, getNothingException { + ArrayList> readData = new ArrayList<>(); + try (Scanner scanner = new Scanner(new File(args[0]))) { + while (scanner.hasNextLine()) { + Integer value = tryParse(scanner.nextLine()); + if (value == null) { + readData.add(Maybe.nothing()); + } else { + readData.add(Maybe.just(value)); + } + } + } + + ArrayList> squaredNumber = new ArrayList<>(); + for (Maybe maybeInt : readData) { + squaredNumber.add(maybeInt.map(value -> value * value)); + } + + try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(args[1]))) { + for (Maybe maybeInt : squaredNumber) { + if (maybeInt.isPresent()) { + bufferedWriter.write(maybeInt.get().toString()); + } else { + bufferedWriter.write("null"); + } + bufferedWriter.newLine(); + } + } + } + + /** + * Parses specified String to Integer. + * + * @return Integer if String was a number, and null otherwise + */ + @Nullable + private static Integer tryParse(@NotNull String parsingString) { + try { + return Integer.parseInt(parsingString); + } catch (NumberFormatException e) { + return null; + } + } + +} diff --git a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java new file mode 100644 index 0000000..b0216f4 --- /dev/null +++ b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java @@ -0,0 +1,83 @@ +package ru.spbau.mit.kazakov.Maybe; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Function; + +/** + * Generic container for storing a value or nothing. + */ +public class Maybe { + @Nullable + private final T value; + + /** + * Assigns specified value to class's storing field. + * + * @param t specified value + */ + private Maybe(T t) { + value = t; + } + + /** + * Creates an object for storing specified value. + * + * @param t specified value + * @return new object + */ + @NotNull + public static Maybe just(T t) { + return new Maybe(t); + } + + /** + * Creates an object for strong nothing. + * + * @return new object + */ + @NotNull + public static Maybe nothing() { + return new Maybe(null); + } + + /** + * Returns stored value or throws exception if there is no value. + * + * @return stored value + * @throws getNothingException if there is no stored value + */ + @NotNull + public T get() throws getNothingException { + if (isPresent()) { + return value; + } else { + throw new getNothingException(); + } + } + + /** + * Checks if there is stored value. + * + * @return true if there is stored value, and false otherwise + */ + public boolean isPresent() { + return value != null; + } + + /** + * Maps stored value of type T to another value of type U. + * + * @param mapper a function acting from T to V + * @param image's type + * @return Maybe object storing mapped value if there is a storing value, and Maybe object storing nothing otherwise + */ + @NotNull + public Maybe map(@NotNull Function mapper) { + if (!isPresent()) { + return nothing(); + } + return just(mapper.apply(value)); + } +} diff --git a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java new file mode 100644 index 0000000..7789bf9 --- /dev/null +++ b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java @@ -0,0 +1,7 @@ +package ru.spbau.mit.kazakov.Maybe; + +/** + * Exception thrown when there is no value to return. + */ +public class getNothingException extends Exception { +} diff --git a/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java b/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java new file mode 100644 index 0000000..90c6fe1 --- /dev/null +++ b/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java @@ -0,0 +1,47 @@ +package ru.spbau.mit.kazakov.Maybe; + +import org.junit.Test; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Scanner; + +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +import static org.junit.Assert.*; + +public class MainTest { + @Rule + public TemporaryFolder testFolder = new TemporaryFolder(); + + @Test + public void testMain() throws IOException, getNothingException { + File input = testFolder.newFile("input.txt"); + File output = testFolder.newFile("output.txt"); + + try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(input))) { + bufferedWriter.write("1\n0\n-3\nj\njava\n1b\n11 22\n"); + } + + Main.main(new String[]{input.getAbsolutePath(), output.getAbsolutePath()}); + + + assertEquals("1\n0\n9\nnull\nnull\nnull\nnull\n", new String(Files.readAllBytes(Paths.get(output.getAbsolutePath())))); + } + + @Test + public void testMainEmpty() throws IOException, getNothingException { + File input = testFolder.newFile("input.txt"); + File output = testFolder.newFile("output.txt"); + + Main.main(new String[]{input.getAbsolutePath(), output.getAbsolutePath()}); + + assertEquals("", new String(Files.readAllBytes(Paths.get(output.getAbsolutePath())))); + } + +} \ No newline at end of file diff --git a/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java b/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java new file mode 100644 index 0000000..54b7846 --- /dev/null +++ b/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java @@ -0,0 +1,60 @@ +package ru.spbau.mit.kazakov.Maybe; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MaybeTest { + @Test + public void testJustInteger() throws getNothingException { + Maybe maybeInteger = Maybe.just(5); + int storedValue = maybeInteger.get(); + assertEquals(5, storedValue); + } + + @Test + public void testNothingBoolean() { + Maybe maybeBoolean = Maybe.nothing(); + assertFalse(maybeBoolean.isPresent()); + } + + @Test + public void testGetDouble() throws getNothingException { + Maybe maybeDouble = Maybe.just(-7.0); + double storedValue = maybeDouble.get(); + assertEquals(-7.0, storedValue, 0.000000001); + } + + @Test(expected = getNothingException.class) + public void testGetCharacterThrowsException() throws getNothingException { + Maybe maybeCharacter = Maybe.nothing(); + char storedValue = maybeCharacter.get(); + } + + @Test + public void testIsPresentFalse() { + Maybe maybeBoolean = Maybe.nothing(); + assertFalse(maybeBoolean.isPresent()); + } + + @Test + public void testIsPresentTrue() { + Maybe maybeString = Maybe.just("abacaba"); + assertTrue(maybeString.isPresent()); + } + + @Test + public void testMapJust() throws getNothingException { + Maybe maybeInteger = Maybe.just(10); + Maybe mappedMaybeInteger = maybeInteger.map(value -> value * value); + int mappedValue = mappedMaybeInteger.get(); + assertEquals(100, mappedValue); + } + + @Test + public void testMapNothing() throws getNothingException { + Maybe maybeInteger = Maybe.nothing(); + Maybe mappedMaybeInteger = maybeInteger.map(value -> value * value); + assertFalse(mappedMaybeInteger.isPresent()); + } +} \ No newline at end of file diff --git a/Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Main.class b/Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..6bafd51dadac3eb34fc19d52644fad85854c4d65 GIT binary patch literal 3978 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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml b/Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml new file mode 100644 index 0000000..8af6efe --- /dev/null +++ b/Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt b/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt new file mode 100644 index 0000000..adbb610 --- /dev/null +++ b/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: ru.spbau.mit.kazakov.Maybe.MainTest +------------------------------------------------------------------------------- +Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.056 sec - in ru.spbau.mit.kazakov.Maybe.MainTest diff --git a/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt b/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt new file mode 100644 index 0000000..15d544e --- /dev/null +++ b/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: ru.spbau.mit.kazakov.Maybe.MaybeTest +------------------------------------------------------------------------------- +Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.07 sec - in ru.spbau.mit.kazakov.Maybe.MaybeTest diff --git a/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MainTest.class b/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MainTest.class new file mode 100644 index 0000000000000000000000000000000000000000..3f48d60837c6182b50031c73aeaf1e02f1a8ebe6 GIT binary patch literal 2225 zcma)7*-{%v6g`b3MvMou8Y4Ri;3aGZ0nTEC?SwUUB9;KbmdQ4f1{fI4$fFr!@{o^6 z-s0C(`6;P#RRUC~#BYAcM}?f85kgSdNr8H~ef#ve=icu5`=3Al0`MM6DlTJGK}tmp zM(c11V{zP;lRI(TRq&C5k5$xRTtyrcaZJj7N;cC9W>hrb69xBF+{dRXK0`W=Srv1b zSFwQ4RWxBy#S)g|_(H*of(HVsXOz4d$I2V7Kv&vv*OQyRZF))9w~SJ9*(ekpS9f

nBUatnMcZWVWS$r*An=lC%0w)?jPe z%Z!0%#bqpVE^t2UudNxbk$>Qto(!!$y{SLZlctlLII+TQG1D&k9`{sSX3UVg<9oqM z{gS8Wwr2HWz$=jMVUUa&3OM~VXA}bg6l4@UIw?b$TKy5V=|a)l;jSgecXNh3NTB{G zhC#_uLnpcfI{qUS8s5U&0(AlTLGP)jVHIBzIV3GD0&QmpO}UR2B!f^RdG1%T`C8!O zusWm;+*CJJ>8B27)#2gJkr7qHH~3bAE(x{-5SDJQCzm%|XIth?Ll!v-aJ;nSxu(6Y z;Gu>*3BEE}R7`*K?Lrk_Dva^GklQ=o+Pxk!P`2`&W_^KsGRf@odSl ze9u_WJ!Yb{r_8XLH0$pTDuj0|S_rOMThHnlLCcr|v2BLA#v)6~(|DO9Bp@(W&9E%g zSui2;2@KxYQ^-<9B5SUp=f^E;VuzRQScH?Cdsio@ThHo*)VJ~Yj*U|}S5}A~45lCZ zxc#-SPGO&6BzbILi}MA(XL2416zQWTKs(z6TB&q5RUV+u3mj+I ze@~r%q$0o2TPoH^?XKYwdeBP*>QmNE#m`*pr-rTg2{$;ZArAOej31ItOu?XnWbi+v zU>JhUxfwS1_&ekmR5|TY@roQ0D^gh|=m>wcG~VQ={iqz?r%f>Uasu%KrH}-t&Pw3f vvL;?H>rnx<5fv0kr2wrAxR;?$GEgZ^XLw#kkW@vGp|T*a=lvn~*WlK_9R~(P literal 0 HcmV?d00001 diff --git a/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MaybeTest.class b/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MaybeTest.class new file mode 100644 index 0000000000000000000000000000000000000000..fe9ccbf84598e037745c1e9f962681c7c236ddc4 GIT binary patch literal 3912 zcmb7HX;;){6nIA23#v>5l37`TkD3>D!5<=wMuDg?Gk1p1cqcxGAP#8Zg$^W zyV)PnexV#c^!T}_pZgp7D|-68Z!(#gf+(CbN#5n&`@GM6?oIgf-+O-m*oR* z{e4I7c{PUDG<4$iMi_EDk)XwtbYYcT=MoT@ieV;(yg;}+9iVZtb;XDvTHW85%ioVoO< zF+X7j*D=%c2k6%4{Y=sGM{M7mG+hCGR&I)hTOR6(UYb4Man0PgkuMUM7!l~I1~*?Q z0X*PvcBn#O}WlBFJeQWRRxRInSmjJL#v7x8LNTnm?*{ch~Ui4jN za<#k$R?2m=bUkVmr1}YZ)+iLroI(`Gq*BX> zc>6MQ0`+r2)pRVonyd~XvG(p^EnIYpZq__$$wttsK#%b>yJh9*=)qNA7qozOQF?^ynP(Uh>lP2sgBR^IVZjl=zWy- zRR!+c#55HvuzLl|W#merivM$0*t1e2%crPk1)qw0cIj!QqWzV546|Q;#U1GTfp2Iq>OI?zFJ5$4zP{Ve`%{kQo%V^p88dAfgODrR zR;nTNJY?ka7cDmO814#eUBb_D=MmVn@;+>40=m^lS2Mqw*y?!Q!f~2+!FzrG?-0N7 zp$3oh+Mp(5=;gB>Y`|9ZaU`&f# zNuRaKXM(!{4UK3<2fc1Ui}K$S_z3+sB8e2XGfoGZ(2pHNw3BucMXsui5g#{+M4quHiIzpQl^KaIpnZgssU#AMNS1K~oU9=$Yx%LejwxDS!AVbuMI*NT z3QH%^_TxYWC&Ai5qIwV~ha!Aa+$HlB4>@73Iv3D&w2UX_gLEfHTRg!uMW-E`~r@%Z~lAd9d>LeYDVqWH& z6h>=eO_(4`W9|Pb=w~W-P@p?2qPB(_LAY-VEoDW>HrCS5-XnigWcgQ4RJOB2 z3QEwvJy1;E?SL~y-S^tnNbz$EPmi!qq{r!3EwlnS&rqYa@=F$3MRv+X-xhH D_sVhV literal 0 HcmV?d00001 From 06c3acd897488ae8f364616d1e995b9c3e1a49ee Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 28 Nov 2017 02:33:33 +0300 Subject: [PATCH 3/5] Initial commit. --- Mock/.gitignore | 49 +++++ Mock/.idea/compiler.xml | 16 ++ Mock/.idea/misc.xml | 11 + Mock/.idea/modules.xml | 8 + Mock/Mock.iml | 19 ++ Mock/pom.xml | 47 +++++ .../arithmetic/ArithmeticCalculator.java | 26 +++ .../kazakov/arithmetic/ArithmeticUtility.java | 158 ++++++++++++++ .../kazakov/arithmetic/ExpressionParser.java | 60 ++++++ .../arithmetic/PolishNotationCalculator.java | 59 ++++++ .../exception/ArithmeticException.java | 10 + .../exception/EmptyStackException.java | 4 + .../java/ru/mit/spbau/kazakov/util/Stack.java | 87 ++++++++ .../arithmetic/ArithmeticUtilityTest.java | 199 ++++++++++++++++++ .../arithmetic/ExpressionParserTest.java | 36 ++++ .../PolishNotationCalculatorTest.java | 100 +++++++++ .../ru/mit/spbau/kazakov/util/StackTest.java | 103 +++++++++ 17 files changed, 992 insertions(+) create mode 100644 Mock/.gitignore create mode 100644 Mock/.idea/compiler.xml create mode 100644 Mock/.idea/misc.xml create mode 100644 Mock/.idea/modules.xml create mode 100644 Mock/Mock.iml create mode 100644 Mock/pom.xml create mode 100644 Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticCalculator.java create mode 100644 Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtility.java create mode 100644 Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParser.java create mode 100644 Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java create mode 100644 Mock/src/main/java/ru/mit/spbau/kazakov/exception/ArithmeticException.java create mode 100644 Mock/src/main/java/ru/mit/spbau/kazakov/exception/EmptyStackException.java create mode 100644 Mock/src/main/java/ru/mit/spbau/kazakov/util/Stack.java create mode 100644 Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtilityTest.java create mode 100644 Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParserTest.java create mode 100644 Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculatorTest.java create mode 100644 Mock/src/test/java/ru/mit/spbau/kazakov/util/StackTest.java diff --git a/Mock/.gitignore b/Mock/.gitignore new file mode 100644 index 0000000..345e61a --- /dev/null +++ b/Mock/.gitignore @@ -0,0 +1,49 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/Mock/.idea/compiler.xml b/Mock/.idea/compiler.xml new file mode 100644 index 0000000..691ff71 --- /dev/null +++ b/Mock/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Mock/.idea/misc.xml b/Mock/.idea/misc.xml new file mode 100644 index 0000000..9896aeb --- /dev/null +++ b/Mock/.idea/misc.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/Mock/.idea/modules.xml b/Mock/.idea/modules.xml new file mode 100644 index 0000000..10cf8c4 --- /dev/null +++ b/Mock/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Mock/Mock.iml b/Mock/Mock.iml new file mode 100644 index 0000000..690021f --- /dev/null +++ b/Mock/Mock.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Mock/pom.xml b/Mock/pom.xml new file mode 100644 index 0000000..84bb5ee --- /dev/null +++ b/Mock/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + ru.spbau.mit.kazakov + Mock + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + junit + junit + 4.8 + + + org.jetbrains + annotations + 13.0 + + + com.google.guava + guava + r05 + + + org.mockito + mockito-all + 1.9.5 + test + + + + \ No newline at end of file diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticCalculator.java b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticCalculator.java new file mode 100644 index 0000000..b284499 --- /dev/null +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticCalculator.java @@ -0,0 +1,26 @@ +package ru.mit.spbau.kazakov.arithmetic; + +import ru.mit.spbau.kazakov.exception.ArithmeticException; +import ru.mit.spbau.kazakov.util.Stack; + +import java.util.List; + +/** + * A class for evaluating arithmetic expressions. + * Operators and operands must be separated by whitespace character. + * Supported operators: +, -, *, /, %, ^ + * Don't use brackets for unary minus. + */ +public class ArithmeticCalculator { + public static void main(String[] args) { + String expression = String.join(" ", args); + PolishNotationCalculator calculator = new PolishNotationCalculator(new Stack<>()); + + try { + List polishNotation = ExpressionParser.toPolishNotation(expression); + System.out.println(calculator.evaluate(polishNotation)); + } catch (ArithmeticException exception) { + System.out.println(exception.getMessage()); + } + } +} diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtility.java b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtility.java new file mode 100644 index 0000000..34c3f9e --- /dev/null +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtility.java @@ -0,0 +1,158 @@ +package ru.mit.spbau.kazakov.arithmetic; + +import org.jetbrains.annotations.NotNull; +import ru.mit.spbau.kazakov.exception.ArithmeticException; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +/** + * A class with arithmetic utilities. + */ +public class ArithmeticUtility { + private static final Map OPERATORS = new HashMap<>(); + + static { + OPERATORS.put("+", Operator.ADDITION); + OPERATORS.put("-", Operator.SUBTRACTION); + OPERATORS.put("*", Operator.MULTIPLICATION); + OPERATORS.put("/", Operator.DIVISION); + OPERATORS.put("%", Operator.MODULO); + OPERATORS.put("^", Operator.EXPONENTIATION); + } + + private enum Operator { + ADDITION(0, true) { + @Override + public double apply(double augend, double addend) { + return augend + addend; + } + }, + SUBTRACTION(0, true) { + @Override + public double apply(double minuend, double subtrahend) { + return minuend - subtrahend; + } + }, + MULTIPLICATION(5, true) { + @Override + public double apply(double multiplier, double multiplicand) { + return multiplier * multiplicand; + } + }, + DIVISION(5, true) { + @Override + public double apply(double dividend, double divisor) throws ArithmeticException { + if (divisor == 0) { + throw new ArithmeticException("Division by zero"); + } + return dividend / divisor; + } + }, + MODULO(5, true) { + @Override + public double apply(double dividend, double divisor) { + return dividend % divisor; + } + }, + EXPONENTIATION(10, false) { + @Override + public double apply(double base, double exponent) { + return Math.pow(base, exponent); + } + }; + + private final int precedence; + private final boolean isLeftAssociative; + + Operator(int precedence, boolean isLeftAssociative) { + this.precedence = precedence; + this.isLeftAssociative = isLeftAssociative; + } + + public abstract double apply(double operand1, double operand2) throws ArithmeticException; + } + + private static final Pattern IS_NUMERIC = Pattern.compile("[+-]?\\d+(\\.\\d+)?"); + + /** + * Checks if specified token is a number. + * + * @param token to check + * @return true if specified token is a number, and false otherwise + */ + public static boolean isNumeric(@NotNull String token) { + return IS_NUMERIC.matcher(token).matches(); + } + + /** + * Checks if specified token is an operator. + * + * @param token to check + * @return true if specified token is an operator, and false otherwise + */ + public static boolean isOperator(@NotNull String token) { + return OPERATORS.containsKey(token); + } + + /** + * Checks if specified operator is left associative. + * + * @param operator to check + * @return true if specified operator is left associative, and false otherwise + */ + public static boolean isLeftAssociative(@NotNull String operator) throws ArithmeticException { + if (!ArithmeticUtility.isOperator(operator)) { + throw new ArithmeticException("Unsupported operator: " + operator); + } + return OPERATORS.get(operator).isLeftAssociative; + } + + /** + * Compares precedences of specified operators. + * + * @param operator1 the first operator + * @param operator2 the second operator + * @return negative number if first operator's precedence is smaller than second operator's precedence, + * zero if precedences are equal, and positive number otherwise. + */ + public static int comparePrecedences(@NotNull String operator1, @NotNull String operator2) throws ArithmeticException { + if (!isOperator(operator1)) { + throw new ArithmeticException("Unsupported operator: " + operator1); + } + if (!isOperator(operator2)) { + throw new ArithmeticException("Unsupported operator: " + operator2); + } + return OPERATORS.get(operator1).precedence - OPERATORS.get(operator2).precedence; + } + + /** + * Determines which of two operators evaluates first. + * + * @param operator1 the first operator + * @param operator2 the second operator + * @return true if the second operator evaluates before the first one, and false otherwise + * @throws ArithmeticException if there is unsupported operator + */ + public static boolean isGoesAfter(@NotNull String operator1, @NotNull String operator2) throws ArithmeticException { + return (isLeftAssociative(operator1) && comparePrecedences(operator1, operator2) <= 0) + || (!isLeftAssociative(operator1) && comparePrecedences(operator1, operator2) < 0); + } + + /** + * Applies specified operator to specified arguments. + * + * @param operator operator for applying + * @param operand1 the first argument + * @param operand2 the second argument + * @return result of application + * @throws ArithmeticException if there is unsupported operator + */ + public static double apply(@NotNull String operator, double operand1, double operand2) throws ArithmeticException { + if (!isOperator(operator)) { + throw new ArithmeticException("Unsupported operator: " + operator); + } + return OPERATORS.get(operator).apply(operand1, operand2); + } +} diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParser.java b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParser.java new file mode 100644 index 0000000..6d1a1ea --- /dev/null +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParser.java @@ -0,0 +1,60 @@ +package ru.mit.spbau.kazakov.arithmetic; + +import org.jetbrains.annotations.NotNull; +import ru.mit.spbau.kazakov.util.Stack; +import ru.mit.spbau.kazakov.exception.ArithmeticException; + +import java.util.ArrayList; +import java.util.List; + +/** + * A class for parsing mathematical expressions. + * Operators and operands must be separated by whitespace character. + * Supported operators: +, -, *, /, %, ^ + * Don't use brackets for unary minus. + */ +public class ExpressionParser { + /** + * Parses specified expression to Polish notation. + * + * @param expression to parse + * @return resulting stack + * @throws ArithmeticException if input is invalid + */ + public static List toPolishNotation(@NotNull String expression) throws ArithmeticException { + if (expression.trim().length() == 0) { + throw new ArithmeticException("Empty expression"); + } + + ArrayList polishNotation = new ArrayList<>(); + Stack processingStack = new Stack<>(); + + for (String token : expression.trim().split("\\s+")) { + if (ArithmeticUtility.isOperator(token)) { + while (!processingStack.empty() && ArithmeticUtility.isOperator(processingStack.peek()) + && ArithmeticUtility.isGoesAfter(token, processingStack.peek())) { + polishNotation.add(processingStack.pop()); + } + processingStack.push(token); + } else if (token.equals("(")) { + processingStack.push("("); + } else if (token.equals(")")) { + while (!processingStack.empty() && !processingStack.peek().equals("(")) { + polishNotation.add(processingStack.pop()); + } + processingStack.pop(); + } else if (ArithmeticUtility.isNumeric(token)) { + polishNotation.add(token); + } else { + throw new ArithmeticException("Unsupported operator: " + token); + } + } + + while (!processingStack.empty()) { + polishNotation.add(processingStack.pop()); + } + + return polishNotation; + } + +} diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java new file mode 100644 index 0000000..054e72f --- /dev/null +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java @@ -0,0 +1,59 @@ +package ru.mit.spbau.kazakov.arithmetic; + +import com.google.common.collect.Iterables; +import org.jetbrains.annotations.NotNull; +import ru.mit.spbau.kazakov.exception.ArithmeticException; +import ru.mit.spbau.kazakov.util.Stack; + +import java.util.List; + +/** + * A class for evaluating expressions in Polish notation. + * Supported operators: +, -, *, /, %, ^ + */ +public class PolishNotationCalculator { + /** + * Initializes processing stack. + * + * @param processingStack a stack for evaluating expression + */ + public PolishNotationCalculator(@NotNull Stack processingStack) { + this.processingStack = processingStack; + } + + /** + * Evaluates specified expression. + * + * @param polishNotation an expression in Polish notation + * @return value of the evaluated expression + */ + public double evaluate(@NotNull List polishNotation) throws ArithmeticException { + if (polishNotation.size() == 0) { + throw new ArithmeticException("Empty expression"); + } + + processingStack.push(Double.parseDouble(polishNotation.get(0))); + processingStack.push(Double.parseDouble(polishNotation.get(1))); + + for (String token : Iterables.skip(polishNotation, 2)) { + if (ArithmeticUtility.isOperator(token)) { + double operand2 = processingStack.pop(); + double operand1 = processingStack.pop(); + double operationResult = ArithmeticUtility.apply(token, operand1, operand2); + processingStack.push(operationResult); + } else if (ArithmeticUtility.isNumeric(token)) { + processingStack.push(Double.parseDouble(token)); + } else { + throw new ArithmeticException("Unsupported operator: " + token); + } + } + + if (processingStack.size() != 1) { + throw new ArithmeticException("Invalid expression"); + } + + return processingStack.pop(); + } + + private Stack processingStack; +} diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/exception/ArithmeticException.java b/Mock/src/main/java/ru/mit/spbau/kazakov/exception/ArithmeticException.java new file mode 100644 index 0000000..4b5b6f2 --- /dev/null +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/exception/ArithmeticException.java @@ -0,0 +1,10 @@ +package ru.mit.spbau.kazakov.exception; + +/** + * Thrown when unsupported arithmetic operation called. + */ +public class ArithmeticException extends Exception { + public ArithmeticException(String message) { + super(message); + } +} diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/exception/EmptyStackException.java b/Mock/src/main/java/ru/mit/spbau/kazakov/exception/EmptyStackException.java new file mode 100644 index 0000000..8275884 --- /dev/null +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/exception/EmptyStackException.java @@ -0,0 +1,4 @@ +package ru.mit.spbau.kazakov.exception; + +public class EmptyStackException extends RuntimeException { +} diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/util/Stack.java b/Mock/src/main/java/ru/mit/spbau/kazakov/util/Stack.java new file mode 100644 index 0000000..b12a848 --- /dev/null +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/util/Stack.java @@ -0,0 +1,87 @@ +package ru.mit.spbau.kazakov.util; + +import ru.mit.spbau.kazakov.exception.EmptyStackException; + +/** + * An implementation of stack interface for arithmetic calculator. + * + * @param type of stored data + */ +public class Stack { + /** + * Pushes new element to the top of stack. + * + * @param value to push + * @return pushed value + */ + public T push(T value) { + top = new Node<>(value, top); + size++; + + return value; + } + + /** + * Returns top element. + */ + public T peek() { + if (empty()) { + throw new EmptyStackException(); + } + return top.value; + } + + /** + * Deletes top element from stack. + * + * @return deleted element + */ + public T pop() { + if (empty()) { + throw new EmptyStackException(); + } + + T returnValue = top.value; + top = top.prev; + size--; + + return returnValue; + } + + /** + * Checks if stack is empty. + * + * @return true if stack is empty, and false otherwise. + */ + public boolean empty() { + return top == null; + } + + public int size() { + return size; + } + + private Node top = null; + private int size = 0; + + /** + * A stack entry for storing a value and previous entry. + * + * @param type of stored data + */ + private static class Node { + private T value; + private Node prev; + + /** + * Initializes stored value and reference to previous Node. + * + * @param value to initialize with + * @param prev a previous Node + */ + Node(T value, Node prev) { + this.value = value; + this.prev = prev; + } + } +} diff --git a/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtilityTest.java b/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtilityTest.java new file mode 100644 index 0000000..cef8080 --- /dev/null +++ b/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ArithmeticUtilityTest.java @@ -0,0 +1,199 @@ +package ru.mit.spbau.kazakov.arithmetic; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import ru.mit.spbau.kazakov.exception.ArithmeticException; + +import static org.junit.Assert.*; + +public class ArithmeticUtilityTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testIsNumericEmpty() { + assertFalse(ArithmeticUtility.isNumeric("")); + } + + @Test + public void testIsNumericMinus() { + assertFalse(ArithmeticUtility.isNumeric("+")); + } + + @Test + public void testIsNumericPlus() { + assertFalse(ArithmeticUtility.isNumeric("-")); + } + + @Test + public void testIsNumericInt() { + assertTrue(ArithmeticUtility.isNumeric("54576545332")); + } + + @Test + public void testIsNumericDouble() { + assertTrue(ArithmeticUtility.isNumeric("32312.54576545332")); + } + + @Test + public void testIsOperatorPlus() { + assertTrue(ArithmeticUtility.isOperator("+")); + } + + @Test + public void testIsOperatorMinus() { + assertTrue(ArithmeticUtility.isOperator("-")); + } + + @Test + public void testIsOperatorMultiply() { + assertTrue(ArithmeticUtility.isOperator("*")); + } + + @Test + public void testIsOperatorDivide() { + assertTrue(ArithmeticUtility.isOperator("/")); + } + + @Test + public void testIsOperatorModulo() { + assertTrue(ArithmeticUtility.isOperator("%")); + } + + @Test + public void testIsOperatorPow() { + assertTrue(ArithmeticUtility.isOperator("^")); + } + + @Test + public void testIsOperatorIncrement() { + assertFalse(ArithmeticUtility.isOperator("++")); + } + + @Test + public void testIsOperatorXor() { + assertFalse(ArithmeticUtility.isOperator("xor")); + } + + @Test + public void testIsLeftAssociativeThrowsArithmeticException() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Unsupported operator: xor"); + ArithmeticUtility.isLeftAssociative("xor"); + } + + @Test + public void testIsLeftAssociativePlus() throws ArithmeticException { + assertTrue(ArithmeticUtility.isLeftAssociative("+")); + } + + @Test + public void testIsLeftAssociativeMinus() throws ArithmeticException { + assertTrue(ArithmeticUtility.isLeftAssociative("-")); + } + + @Test + public void testIsLeftAssociativeMultiply() throws ArithmeticException { + assertTrue(ArithmeticUtility.isLeftAssociative("*")); + } + + @Test + public void testIsLeftAssociativeDivide() throws ArithmeticException { + assertTrue(ArithmeticUtility.isLeftAssociative("/")); + } + + @Test + public void testIsLeftAssociativeModulo() throws ArithmeticException { + assertTrue(ArithmeticUtility.isLeftAssociative("%")); + } + + @Test + public void testIsLeftAssociativePow() throws ArithmeticException { + assertFalse(ArithmeticUtility.isLeftAssociative("^")); + } + + @Test + public void testComparePrecedencesThrowsArithmeticException() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Unsupported operator: ++"); + ArithmeticUtility.comparePrecedences("++", "-"); + } + + @Test + public void testComparePrecedencesEqual() throws ArithmeticException { + assertTrue(ArithmeticUtility.comparePrecedences("+", "-") == 0); + } + + @Test + public void testComparePrecedencesLess() throws ArithmeticException { + assertTrue(ArithmeticUtility.comparePrecedences("+", "*") < 0); + } + + @Test + public void testComparePrecedencesGreater() throws ArithmeticException { + assertTrue(ArithmeticUtility.comparePrecedences("^", "%") > 0); + } + + @Test + public void testIsGoesAfterThrowsArithmeticException() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Unsupported operator: **"); + ArithmeticUtility.isGoesAfter("+", "**"); + } + + @Test + public void testIsGoesAfterTrue() throws ArithmeticException { + assertTrue(ArithmeticUtility.isGoesAfter("+", "%")); + } + + @Test + public void testIsGoesAfterFalse() throws ArithmeticException { + assertFalse(ArithmeticUtility.isGoesAfter("/", "-")); + } + + @Test + public void testApplyThrowsArithmeticException() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Unsupported operator: xor"); + ArithmeticUtility.apply("xor", 5.0, 0.5); + } + + @Test + public void testApplyPlus() throws ArithmeticException { + assertEquals(5.5, ArithmeticUtility.apply("+", 5.0, 0.5), 0.000001); + } + + @Test + public void testApplyMinus() throws ArithmeticException { + assertEquals(103.3, ArithmeticUtility.apply("-", 103.4, 0.1), 0.000001); + } + + @Test + public void testApplyMultiply() throws ArithmeticException { + assertEquals(10.0, ArithmeticUtility.apply("*", 5.0, 2.0), 0.000001); + } + + @Test + public void testApplyDivide() throws ArithmeticException { + assertEquals(5.0, ArithmeticUtility.apply("/", 40.0, 8.0), 0.000001); + } + + @Test + public void testApplyDivideThrowsArithmeticException() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Division by zero"); + assertEquals(5.0, ArithmeticUtility.apply("/", 40.0, 0.0), 0.000001); + } + + @Test + public void testApplyModulo() throws ArithmeticException { + assertEquals(1.0, ArithmeticUtility.apply("%", 15.0, 7.0), 0.000001); + } + + @Test + public void testApplyPow() throws ArithmeticException { + assertEquals(64.0, ArithmeticUtility.apply("^", 2.0, 6.0), 0.000001); + } + +} \ No newline at end of file diff --git a/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParserTest.java b/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParserTest.java new file mode 100644 index 0000000..e0734fb --- /dev/null +++ b/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/ExpressionParserTest.java @@ -0,0 +1,36 @@ +package ru.mit.spbau.kazakov.arithmetic; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import ru.mit.spbau.kazakov.exception.ArithmeticException; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class ExpressionParserTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testToPolishNotation1() throws ArithmeticException { + List polishNotation = ExpressionParser.toPolishNotation("( 1 + 2 ) * ( 3 / 4 ) ^ ( 5 + 6 )"); + assertEquals(Arrays.asList("1", "2", "+", "3", "4", "/", "5", "6", "+", "^", "*"), polishNotation); + } + + @Test + public void testToPolishNotation2() throws ArithmeticException { + List polishNotation = ExpressionParser.toPolishNotation("( -4 + 6 ) % 3 - ( 5 - 4 ) * ( 11 + 54 )"); + assertEquals(Arrays.asList("-4", "6", "+", "3", "%", "5", "4", "-", "11", "54", "+", "*", "-"), polishNotation); + } + + @Test + public void testToPolishNotationThrowsArithmeticException() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Unsupported operator: xor"); + ExpressionParser.toPolishNotation("( 1 + 2 ) * ( 3 / 4 ) ^ ( 5 xor 6 )"); + } + +} \ No newline at end of file diff --git a/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculatorTest.java b/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculatorTest.java new file mode 100644 index 0000000..29e5c09 --- /dev/null +++ b/Mock/src/test/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculatorTest.java @@ -0,0 +1,100 @@ +package ru.mit.spbau.kazakov.arithmetic; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.InOrder; +import ru.mit.spbau.kazakov.exception.ArithmeticException; +import ru.mit.spbau.kazakov.util.Stack; + +import java.util.Arrays; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +public class PolishNotationCalculatorTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + @SuppressWarnings("unchecked") + public void testEvaluateEasy() throws ArithmeticException { + Stack stack = mock(Stack.class); + when(stack.pop()).thenReturn(2.0, -3.0, -1.0); + when(stack.size()).thenReturn(1); + + PolishNotationCalculator calculator = new PolishNotationCalculator(stack); + assertEquals(-1.0, + calculator.evaluate(Arrays.asList("-3", "2", "%")), 0.00001); + + InOrder inOrder = inOrder(stack); + inOrder.verify(stack).push(-3.0); + inOrder.verify(stack).push(2.0); + inOrder.verify(stack, times(2)).pop(); + inOrder.verify(stack).push(-1.0); + inOrder.verify(stack).size(); + inOrder.verify(stack).pop(); + } + + @Test + @SuppressWarnings("unchecked") + public void testEvaluateMedium() throws ArithmeticException { + Stack stack = mock(Stack.class); + when(stack.pop()).thenReturn(2.0, 1.0, 3.0, 4.0, 1.3333333333333333, 3.0, 6.0, 5.0, 11.0, 4.0, 4194304.0); + when(stack.size()).thenReturn(1); + + PolishNotationCalculator calculator = new PolishNotationCalculator(stack); + assertEquals(4194304.0, + calculator.evaluate(Arrays.asList("1", "2", "+", "4", "3", "/", "*", "5", "6", "+", "^")), 0.00001); + + InOrder inOrder = inOrder(stack); + inOrder.verify(stack).push(1.0); + inOrder.verify(stack).push(2.0); + inOrder.verify(stack, times(2)).pop(); + inOrder.verify(stack).push(3.0); + inOrder.verify(stack).push(4.0); + inOrder.verify(stack).push(3.0); + inOrder.verify(stack, times(2)).pop(); + inOrder.verify(stack).push(1.3333333333333333); + inOrder.verify(stack, times(2)).pop(); + inOrder.verify(stack).push(4.0); + inOrder.verify(stack).push(5.0); + inOrder.verify(stack).push(6.0); + inOrder.verify(stack, times(2)).pop(); + inOrder.verify(stack).push(11.0); + inOrder.verify(stack, times(2)).pop(); + inOrder.verify(stack).push(4194304.0); + inOrder.verify(stack).size(); + inOrder.verify(stack).pop(); + } + + @Test + @SuppressWarnings("unchecked") + public void testEvaluateInvalidExpression() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Invalid expression"); + + Stack stack = mock(Stack.class); + when(stack.pop()).thenReturn(2.0, 1.0, 1.0, 5.0); + when(stack.size()).thenReturn(2); + + PolishNotationCalculator calculator = new PolishNotationCalculator(stack); + calculator.evaluate(Arrays.asList("1", "2", "%", "5")); + + } + + @Test + @SuppressWarnings("unchecked") + public void testEvaluateUnsupportedOperator() throws ArithmeticException { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Unsupported operator: div"); + + Stack stack = mock(Stack.class); + when(stack.pop()).thenReturn(2.0, 1.0); + + PolishNotationCalculator calculator = new PolishNotationCalculator(stack); + calculator.evaluate(Arrays.asList("1", "2", "div")); + + } + +} \ No newline at end of file diff --git a/Mock/src/test/java/ru/mit/spbau/kazakov/util/StackTest.java b/Mock/src/test/java/ru/mit/spbau/kazakov/util/StackTest.java new file mode 100644 index 0000000..e8d99b3 --- /dev/null +++ b/Mock/src/test/java/ru/mit/spbau/kazakov/util/StackTest.java @@ -0,0 +1,103 @@ +package ru.mit.spbau.kazakov.util; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import ru.mit.spbau.kazakov.exception.EmptyStackException; + +import static org.junit.Assert.*; + +public class StackTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testConstructor() { + new Stack<>(); + } + + @Test + public void testPushReturnValue() { + Stack stack = new Stack<>(); + assertEquals("abc", stack.push("abc")); + } + + @Test + public void testPeek() { + Stack stack = new Stack<>(); + stack.push("xyz"); + assertEquals("xyz", stack.peek()); + } + + @Test + public void testOneHundredPeek() { + Stack stack = new Stack<>(); + for (int i = 0; i < 100; i++) { + stack.push(Integer.toString(i)); + assertEquals(Integer.toString(i), stack.peek()); + } + } + + @Test + public void testPopThrowsEmptyStackException() throws EmptyStackException { + thrown.expect(EmptyStackException.class); + new Stack<>().pop(); + } + + @Test + public void testPopReturnValue() throws EmptyStackException { + Stack stack = new Stack<>(); + stack.push("."); + assertEquals(".", stack.pop()); + } + + @Test + public void testSizeEmpty() { + assertEquals(0, new Stack<>().size()); + } + + @Test + public void testSizeOnePushed() { + Stack stack = new Stack<>(); + stack.push(33); + assertEquals(1, stack.size()); + } + + @Test + public void testSizeOneHundredPushed() { + Stack stack = new Stack<>(); + for (int i = 0; i < 100; i++) { + stack.push(i); + } + assertEquals(100, stack.size()); + } + + @Test + public void testSizeAfterPop() { + Stack stack = new Stack<>(); + stack.push(21); + stack.push(76); + stack.pop(); + assertEquals(1, stack.size()); + } + + @Test + public void testEmptyEmpty() { + assertTrue(new Stack<>().empty()); + } + + @Test + public void testEmptyAfterPush() { + Stack stack = new Stack<>(); + stack.push("r"); + assertFalse(stack.empty()); + } + + @Test + public void testEmptyAfterPop() { + Stack stack = new Stack<>(); + stack.push("r"); + stack.pop(); + assertTrue(stack.empty()); + } +} \ No newline at end of file From 9d5fe615d0431e1b0b36c02135a454194671e8db Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 28 Nov 2017 02:35:22 +0300 Subject: [PATCH 4/5] Removed extra folders --- Untitled Folder 2/Java/.gitignore | 49 ----------- Untitled Folder 2/Java/.idea/compiler.xml | 16 ---- Untitled Folder 2/Java/.idea/misc.xml | 13 --- Untitled Folder 2/Java/.idea/modules.xml | 8 -- Untitled Folder 2/Java/Maybe.iml | 17 ---- Untitled Folder 2/Java/pom.xml | 36 -------- .../java/ru/spbau/mit/kazakov/Maybe/Main.java | 63 ------------- .../ru/spbau/mit/kazakov/Maybe/Maybe.java | 83 ------------------ .../kazakov/Maybe/getNothingException.java | 7 -- .../ru/spbau/mit/kazakov/Maybe/MainTest.java | 47 ---------- .../ru/spbau/mit/kazakov/Maybe/MaybeTest.java | 60 ------------- .../ru/spbau/mit/kazakov/Maybe/Main.class | Bin 3978 -> 0 bytes .../ru/spbau/mit/kazakov/Maybe/Maybe.class | Bin 2100 -> 0 bytes .../kazakov/Maybe/getNothingException.class | Bin 339 -> 0 bytes .../compile/default-compile/createdFiles.lst | 3 - .../compile/default-compile/inputFiles.lst | 3 - .../default-testCompile/createdFiles.lst | 2 - .../default-testCompile/inputFiles.lst | 2 - ...ST-ru.spbau.mit.kazakov.Maybe.MainTest.xml | 64 -------------- ...T-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml | 70 --------------- .../ru.spbau.mit.kazakov.Maybe.MainTest.txt | 4 - .../ru.spbau.mit.kazakov.Maybe.MaybeTest.txt | 4 - .../ru/spbau/mit/kazakov/Maybe/MainTest.class | Bin 2225 -> 0 bytes .../spbau/mit/kazakov/Maybe/MaybeTest.class | Bin 3912 -> 0 bytes kek2/Java | 1 - 25 files changed, 552 deletions(-) delete mode 100644 Untitled Folder 2/Java/.gitignore delete mode 100644 Untitled Folder 2/Java/.idea/compiler.xml delete mode 100644 Untitled Folder 2/Java/.idea/misc.xml delete mode 100644 Untitled Folder 2/Java/.idea/modules.xml delete mode 100644 Untitled Folder 2/Java/Maybe.iml delete mode 100644 Untitled Folder 2/Java/pom.xml delete mode 100644 Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java delete mode 100644 Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java delete mode 100644 Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java delete mode 100644 Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java delete mode 100644 Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java delete mode 100644 Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Main.class delete mode 100644 Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Maybe.class delete mode 100644 Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/getNothingException.class delete mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst delete mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst delete mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst delete mode 100644 Untitled Folder 2/Java/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst delete mode 100644 Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MainTest.xml delete mode 100644 Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml delete mode 100644 Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt delete mode 100644 Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt delete mode 100644 Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MainTest.class delete mode 100644 Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MaybeTest.class delete mode 160000 kek2/Java diff --git a/Untitled Folder 2/Java/.gitignore b/Untitled Folder 2/Java/.gitignore deleted file mode 100644 index 345e61a..0000000 --- a/Untitled Folder 2/Java/.gitignore +++ /dev/null @@ -1,49 +0,0 @@ -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff: -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/dictionaries - -# Sensitive or high-churn files: -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.xml -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml - -# Gradle: -.idea/**/gradle.xml -.idea/**/libraries - -# CMake -cmake-build-debug/ - -# Mongo Explorer plugin: -.idea/**/mongoSettings.xml - -## File-based project format: -*.iws - -## Plugin-specific files: - -# IntelliJ -out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Cursive Clojure plugin -.idea/replstate.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties diff --git a/Untitled Folder 2/Java/.idea/compiler.xml b/Untitled Folder 2/Java/.idea/compiler.xml deleted file mode 100644 index 067d5ff..0000000 --- a/Untitled Folder 2/Java/.idea/compiler.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Untitled Folder 2/Java/.idea/misc.xml b/Untitled Folder 2/Java/.idea/misc.xml deleted file mode 100644 index e8942bd..0000000 --- a/Untitled Folder 2/Java/.idea/misc.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/Untitled Folder 2/Java/.idea/modules.xml b/Untitled Folder 2/Java/.idea/modules.xml deleted file mode 100644 index 65c89bb..0000000 --- a/Untitled Folder 2/Java/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/Untitled Folder 2/Java/Maybe.iml b/Untitled Folder 2/Java/Maybe.iml deleted file mode 100644 index 57f498f..0000000 --- a/Untitled Folder 2/Java/Maybe.iml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Untitled Folder 2/Java/pom.xml b/Untitled Folder 2/Java/pom.xml deleted file mode 100644 index fe36c91..0000000 --- a/Untitled Folder 2/Java/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - 4.0.0 - - ru.spbau.mit.kazakov.Maybe - Maybe - 1.0-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - - - - - junit - junit - 4.8 - - - org.jetbrains - annotations - 13.0 - - - - \ No newline at end of file diff --git a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java deleted file mode 100644 index 8bd1447..0000000 --- a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java +++ /dev/null @@ -1,63 +0,0 @@ -package ru.spbau.mit.kazakov.Maybe; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.*; -import java.util.ArrayList; -import java.util.Scanner; - -/** - * Reads numbers from input file and writes squared values to output file. If read data isn't a number writes "null" instead. - */ -public class Main { - /** - * Primary method. - * - * @param args the first argument is input file and the second one is output file - */ - public static void main(@NotNull String[] args) throws IOException, getNothingException { - ArrayList> readData = new ArrayList<>(); - try (Scanner scanner = new Scanner(new File(args[0]))) { - while (scanner.hasNextLine()) { - Integer value = tryParse(scanner.nextLine()); - if (value == null) { - readData.add(Maybe.nothing()); - } else { - readData.add(Maybe.just(value)); - } - } - } - - ArrayList> squaredNumber = new ArrayList<>(); - for (Maybe maybeInt : readData) { - squaredNumber.add(maybeInt.map(value -> value * value)); - } - - try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(args[1]))) { - for (Maybe maybeInt : squaredNumber) { - if (maybeInt.isPresent()) { - bufferedWriter.write(maybeInt.get().toString()); - } else { - bufferedWriter.write("null"); - } - bufferedWriter.newLine(); - } - } - } - - /** - * Parses specified String to Integer. - * - * @return Integer if String was a number, and null otherwise - */ - @Nullable - private static Integer tryParse(@NotNull String parsingString) { - try { - return Integer.parseInt(parsingString); - } catch (NumberFormatException e) { - return null; - } - } - -} diff --git a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java deleted file mode 100644 index b0216f4..0000000 --- a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java +++ /dev/null @@ -1,83 +0,0 @@ -package ru.spbau.mit.kazakov.Maybe; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.function.Function; - -/** - * Generic container for storing a value or nothing. - */ -public class Maybe { - @Nullable - private final T value; - - /** - * Assigns specified value to class's storing field. - * - * @param t specified value - */ - private Maybe(T t) { - value = t; - } - - /** - * Creates an object for storing specified value. - * - * @param t specified value - * @return new object - */ - @NotNull - public static Maybe just(T t) { - return new Maybe(t); - } - - /** - * Creates an object for strong nothing. - * - * @return new object - */ - @NotNull - public static Maybe nothing() { - return new Maybe(null); - } - - /** - * Returns stored value or throws exception if there is no value. - * - * @return stored value - * @throws getNothingException if there is no stored value - */ - @NotNull - public T get() throws getNothingException { - if (isPresent()) { - return value; - } else { - throw new getNothingException(); - } - } - - /** - * Checks if there is stored value. - * - * @return true if there is stored value, and false otherwise - */ - public boolean isPresent() { - return value != null; - } - - /** - * Maps stored value of type T to another value of type U. - * - * @param mapper a function acting from T to V - * @param image's type - * @return Maybe object storing mapped value if there is a storing value, and Maybe object storing nothing otherwise - */ - @NotNull - public Maybe map(@NotNull Function mapper) { - if (!isPresent()) { - return nothing(); - } - return just(mapper.apply(value)); - } -} diff --git a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java b/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java deleted file mode 100644 index 7789bf9..0000000 --- a/Untitled Folder 2/Java/src/main/java/ru/spbau/mit/kazakov/Maybe/getNothingException.java +++ /dev/null @@ -1,7 +0,0 @@ -package ru.spbau.mit.kazakov.Maybe; - -/** - * Exception thrown when there is no value to return. - */ -public class getNothingException extends Exception { -} diff --git a/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java b/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java deleted file mode 100644 index 90c6fe1..0000000 --- a/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package ru.spbau.mit.kazakov.Maybe; - -import org.junit.Test; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Scanner; - -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; - -import static org.junit.Assert.*; - -public class MainTest { - @Rule - public TemporaryFolder testFolder = new TemporaryFolder(); - - @Test - public void testMain() throws IOException, getNothingException { - File input = testFolder.newFile("input.txt"); - File output = testFolder.newFile("output.txt"); - - try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(input))) { - bufferedWriter.write("1\n0\n-3\nj\njava\n1b\n11 22\n"); - } - - Main.main(new String[]{input.getAbsolutePath(), output.getAbsolutePath()}); - - - assertEquals("1\n0\n9\nnull\nnull\nnull\nnull\n", new String(Files.readAllBytes(Paths.get(output.getAbsolutePath())))); - } - - @Test - public void testMainEmpty() throws IOException, getNothingException { - File input = testFolder.newFile("input.txt"); - File output = testFolder.newFile("output.txt"); - - Main.main(new String[]{input.getAbsolutePath(), output.getAbsolutePath()}); - - assertEquals("", new String(Files.readAllBytes(Paths.get(output.getAbsolutePath())))); - } - -} \ No newline at end of file diff --git a/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java b/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java deleted file mode 100644 index 54b7846..0000000 --- a/Untitled Folder 2/Java/src/test/java/ru/spbau/mit/kazakov/Maybe/MaybeTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package ru.spbau.mit.kazakov.Maybe; - -import org.junit.Test; - -import static org.junit.Assert.*; - -public class MaybeTest { - @Test - public void testJustInteger() throws getNothingException { - Maybe maybeInteger = Maybe.just(5); - int storedValue = maybeInteger.get(); - assertEquals(5, storedValue); - } - - @Test - public void testNothingBoolean() { - Maybe maybeBoolean = Maybe.nothing(); - assertFalse(maybeBoolean.isPresent()); - } - - @Test - public void testGetDouble() throws getNothingException { - Maybe maybeDouble = Maybe.just(-7.0); - double storedValue = maybeDouble.get(); - assertEquals(-7.0, storedValue, 0.000000001); - } - - @Test(expected = getNothingException.class) - public void testGetCharacterThrowsException() throws getNothingException { - Maybe maybeCharacter = Maybe.nothing(); - char storedValue = maybeCharacter.get(); - } - - @Test - public void testIsPresentFalse() { - Maybe maybeBoolean = Maybe.nothing(); - assertFalse(maybeBoolean.isPresent()); - } - - @Test - public void testIsPresentTrue() { - Maybe maybeString = Maybe.just("abacaba"); - assertTrue(maybeString.isPresent()); - } - - @Test - public void testMapJust() throws getNothingException { - Maybe maybeInteger = Maybe.just(10); - Maybe mappedMaybeInteger = maybeInteger.map(value -> value * value); - int mappedValue = mappedMaybeInteger.get(); - assertEquals(100, mappedValue); - } - - @Test - public void testMapNothing() throws getNothingException { - Maybe maybeInteger = Maybe.nothing(); - Maybe mappedMaybeInteger = maybeInteger.map(value -> value * value); - assertFalse(mappedMaybeInteger.isPresent()); - } -} \ No newline at end of file diff --git a/Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Main.class b/Untitled Folder 2/Java/target/classes/ru/spbau/mit/kazakov/Maybe/Main.class deleted file mode 100644 index 6bafd51dadac3eb34fc19d52644fad85854c4d65..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3978 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml b/Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml deleted file mode 100644 index 8af6efe..0000000 --- a/Untitled Folder 2/Java/target/surefire-reports/TEST-ru.spbau.mit.kazakov.Maybe.MaybeTest.xml +++ /dev/null @@ -1,70 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt b/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt deleted file mode 100644 index adbb610..0000000 --- a/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MainTest.txt +++ /dev/null @@ -1,4 +0,0 @@ -------------------------------------------------------------------------------- -Test set: ru.spbau.mit.kazakov.Maybe.MainTest -------------------------------------------------------------------------------- -Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.056 sec - in ru.spbau.mit.kazakov.Maybe.MainTest diff --git a/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt b/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt deleted file mode 100644 index 15d544e..0000000 --- a/Untitled Folder 2/Java/target/surefire-reports/ru.spbau.mit.kazakov.Maybe.MaybeTest.txt +++ /dev/null @@ -1,4 +0,0 @@ -------------------------------------------------------------------------------- -Test set: ru.spbau.mit.kazakov.Maybe.MaybeTest -------------------------------------------------------------------------------- -Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.07 sec - in ru.spbau.mit.kazakov.Maybe.MaybeTest diff --git a/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MainTest.class b/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MainTest.class deleted file mode 100644 index 3f48d60837c6182b50031c73aeaf1e02f1a8ebe6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2225 zcma)7*-{%v6g`b3MvMou8Y4Ri;3aGZ0nTEC?SwUUB9;KbmdQ4f1{fI4$fFr!@{o^6 z-s0C(`6;P#RRUC~#BYAcM}?f85kgSdNr8H~ef#ve=icu5`=3Al0`MM6DlTJGK}tmp zM(c11V{zP;lRI(TRq&C5k5$xRTtyrcaZJj7N;cC9W>hrb69xBF+{dRXK0`W=Srv1b zSFwQ4RWxBy#S)g|_(H*of(HVsXOz4d$I2V7Kv&vv*OQyRZF))9w~SJ9*(ekpS9f

nBUatnMcZWVWS$r*An=lC%0w)?jPe z%Z!0%#bqpVE^t2UudNxbk$>Qto(!!$y{SLZlctlLII+TQG1D&k9`{sSX3UVg<9oqM z{gS8Wwr2HWz$=jMVUUa&3OM~VXA}bg6l4@UIw?b$TKy5V=|a)l;jSgecXNh3NTB{G zhC#_uLnpcfI{qUS8s5U&0(AlTLGP)jVHIBzIV3GD0&QmpO}UR2B!f^RdG1%T`C8!O zusWm;+*CJJ>8B27)#2gJkr7qHH~3bAE(x{-5SDJQCzm%|XIth?Ll!v-aJ;nSxu(6Y z;Gu>*3BEE}R7`*K?Lrk_Dva^GklQ=o+Pxk!P`2`&W_^KsGRf@odSl ze9u_WJ!Yb{r_8XLH0$pTDuj0|S_rOMThHnlLCcr|v2BLA#v)6~(|DO9Bp@(W&9E%g zSui2;2@KxYQ^-<9B5SUp=f^E;VuzRQScH?Cdsio@ThHo*)VJ~Yj*U|}S5}A~45lCZ zxc#-SPGO&6BzbILi}MA(XL2416zQWTKs(z6TB&q5RUV+u3mj+I ze@~r%q$0o2TPoH^?XKYwdeBP*>QmNE#m`*pr-rTg2{$;ZArAOej31ItOu?XnWbi+v zU>JhUxfwS1_&ekmR5|TY@roQ0D^gh|=m>wcG~VQ={iqz?r%f>Uasu%KrH}-t&Pw3f vvL;?H>rnx<5fv0kr2wrAxR;?$GEgZ^XLw#kkW@vGp|T*a=lvn~*WlK_9R~(P diff --git a/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MaybeTest.class b/Untitled Folder 2/Java/target/test-classes/ru/spbau/mit/kazakov/Maybe/MaybeTest.class deleted file mode 100644 index fe9ccbf84598e037745c1e9f962681c7c236ddc4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3912 zcmb7HX;;){6nIA23#v>5l37`TkD3>D!5<=wMuDg?Gk1p1cqcxGAP#8Zg$^W zyV)PnexV#c^!T}_pZgp7D|-68Z!(#gf+(CbN#5n&`@GM6?oIgf-+O-m*oR* z{e4I7c{PUDG<4$iMi_EDk)XwtbYYcT=MoT@ieV;(yg;}+9iVZtb;XDvTHW85%ioVoO< zF+X7j*D=%c2k6%4{Y=sGM{M7mG+hCGR&I)hTOR6(UYb4Man0PgkuMUM7!l~I1~*?Q z0X*PvcBn#O}WlBFJeQWRRxRInSmjJL#v7x8LNTnm?*{ch~Ui4jN za<#k$R?2m=bUkVmr1}YZ)+iLroI(`Gq*BX> zc>6MQ0`+r2)pRVonyd~XvG(p^EnIYpZq__$$wttsK#%b>yJh9*=)qNA7qozOQF?^ynP(Uh>lP2sgBR^IVZjl=zWy- zRR!+c#55HvuzLl|W#merivM$0*t1e2%crPk1)qw0cIj!QqWzV546|Q;#U1GTfp2Iq>OI?zFJ5$4zP{Ve`%{kQo%V^p88dAfgODrR zR;nTNJY?ka7cDmO814#eUBb_D=MmVn@;+>40=m^lS2Mqw*y?!Q!f~2+!FzrG?-0N7 zp$3oh+Mp(5=;gB>Y`|9ZaU`&f# zNuRaKXM(!{4UK3<2fc1Ui}K$S_z3+sB8e2XGfoGZ(2pHNw3BucMXsui5g#{+M4quHiIzpQl^KaIpnZgssU#AMNS1K~oU9=$Yx%LejwxDS!AVbuMI*NT z3QH%^_TxYWC&Ai5qIwV~ha!Aa+$HlB4>@73Iv3D&w2UX_gLEfHTRg!uMW-E`~r@%Z~lAd9d>LeYDVqWH& z6h>=eO_(4`W9|Pb=w~W-P@p?2qPB(_LAY-VEoDW>HrCS5-XnigWcgQ4RJOB2 z3QEwvJy1;E?SL~y-S^tnNbz$EPmi!qq{r!3EwlnS&rqYa@=F$3MRv+X-xhH D_sVhV diff --git a/kek2/Java b/kek2/Java deleted file mode 160000 index d207fbc..0000000 --- a/kek2/Java +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d207fbc8f6316ee94011c17d43e488e48f2e61eb From 5579802d4e97b37bca66e9eb1f9a2dcc5171ee8b Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 28 Nov 2017 20:29:32 +0300 Subject: [PATCH 5/5] Added EmptyStackException handling --- .../arithmetic/PolishNotationCalculator.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java index 054e72f..cec53b0 100644 --- a/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java +++ b/Mock/src/main/java/ru/mit/spbau/kazakov/arithmetic/PolishNotationCalculator.java @@ -3,6 +3,7 @@ import com.google.common.collect.Iterables; import org.jetbrains.annotations.NotNull; import ru.mit.spbau.kazakov.exception.ArithmeticException; +import ru.mit.spbau.kazakov.exception.EmptyStackException; import ru.mit.spbau.kazakov.util.Stack; import java.util.List; @@ -32,27 +33,32 @@ public double evaluate(@NotNull List polishNotation) throws ArithmeticEx throw new ArithmeticException("Empty expression"); } - processingStack.push(Double.parseDouble(polishNotation.get(0))); - processingStack.push(Double.parseDouble(polishNotation.get(1))); - - for (String token : Iterables.skip(polishNotation, 2)) { - if (ArithmeticUtility.isOperator(token)) { - double operand2 = processingStack.pop(); - double operand1 = processingStack.pop(); - double operationResult = ArithmeticUtility.apply(token, operand1, operand2); - processingStack.push(operationResult); - } else if (ArithmeticUtility.isNumeric(token)) { - processingStack.push(Double.parseDouble(token)); - } else { - throw new ArithmeticException("Unsupported operator: " + token); + try { + processingStack.push(Double.parseDouble(polishNotation.get(0))); + processingStack.push(Double.parseDouble(polishNotation.get(1))); + + for (String token : Iterables.skip(polishNotation, 2)) { + if (ArithmeticUtility.isOperator(token)) { + double operand2 = processingStack.pop(); + double operand1 = processingStack.pop(); + double operationResult = ArithmeticUtility.apply(token, operand1, operand2); + processingStack.push(operationResult); + } else if (ArithmeticUtility.isNumeric(token)) { + processingStack.push(Double.parseDouble(token)); + } else { + throw new ArithmeticException("Unsupported operator: " + token); + } + } + + if (processingStack.size() != 1) { + throw new ArithmeticException("Invalid expression"); } - } - if (processingStack.size() != 1) { + return processingStack.pop(); + } + catch (EmptyStackException exception) { throw new ArithmeticException("Invalid expression"); } - - return processingStack.pop(); } private Stack processingStack;