You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package LernProgramm;
  2. import static org.junit.jupiter.api.Assertions.*;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.PrintStream;
  6. import org.junit.jupiter.api.Test;
  7. class testProgramm {
  8. // Testest
  9. @Test
  10. void test() {
  11. assertTrue(true);
  12. }
  13. // PrimZahlen
  14. @Test
  15. public void testPrimBis100() {
  16. ByteArrayOutputStream out = new ByteArrayOutputStream();
  17. System.setOut(new PrintStream(out));
  18. FunktionenAusgelagert.PrimBis100();
  19. assertEquals("2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ", out.toString());
  20. }
  21. // Binärrechner
  22. @Test
  23. public void testAddition() {
  24. int result = FunktionenAusgelagert.calculate("1010", "1011", '+');
  25. assertEquals(10101, result);
  26. }
  27. @Test
  28. public void testSubtraction() {
  29. int result = FunktionenAusgelagert.calculate("1010", "1011", '-');
  30. result++;
  31. if (result < 0 || result > 0) {
  32. assertTrue(true);
  33. }
  34. }
  35. @Test
  36. public void testMultiplication() {
  37. int result = FunktionenAusgelagert.calculate("1010", "1011", '*');
  38. result++;
  39. if (result < 0 || result > 0) {
  40. assertTrue(true);
  41. }
  42. }
  43. @Test
  44. public void testDivision() {
  45. int result = FunktionenAusgelagert.calculate("1010", "1011", '/');
  46. assertEquals(0, result);
  47. }
  48. @Test
  49. public void testInvalidOperation() {
  50. int result = FunktionenAusgelagert.calculate("1010", "1011", '%');
  51. assertEquals(0, result);
  52. }
  53. // Taschenrechner
  54. @Test
  55. public void testAddition1() {
  56. double result = 2 + 3.5;
  57. assertEquals(5.5, result, 0);
  58. }
  59. @Test
  60. public void testSubtraction1() {
  61. double result = 5 - 3.5;
  62. assertEquals(1.5, result, 0);
  63. }
  64. @Test
  65. public void testMultiplication1() {
  66. double result = 5 * 3.5;
  67. assertEquals(17.5, result, 0);
  68. }
  69. @Test
  70. public void testDivision1() {
  71. double result = 15 / 10;
  72. assertEquals(1, result, 0);
  73. }
  74. // Fakultaet
  75. @Test
  76. public void testFakultaetWithPositiveNumber() {
  77. String input = "5\n";
  78. ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
  79. System.setIn(in);
  80. ByteArrayOutputStream out = new ByteArrayOutputStream();
  81. System.setOut(new PrintStream(out));
  82. FunktionenAusgelagert.Fakultaet();
  83. assertEquals("120\n", out.toString());
  84. }
  85. @Test
  86. public void testFakultaetWithZero() {
  87. String input = "0\n";
  88. ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
  89. System.setIn(in);
  90. ByteArrayOutputStream out = new ByteArrayOutputStream();
  91. System.setOut(new PrintStream(out));
  92. FunktionenAusgelagert.Fakultaet();
  93. assertTrue(true);
  94. }
  95. // Schaltjahr
  96. @Test
  97. public void testSchaltjahr() {
  98. ByteArrayInputStream in = new ByteArrayInputStream("2000\n".getBytes());
  99. ByteArrayOutputStream out = new ByteArrayOutputStream();
  100. System.setIn(in);
  101. System.setOut(new PrintStream(out));
  102. FunktionenAusgelagert.schaltjahr();
  103. assertEquals("Welches Jahr möchtest du untersuchen?\nSchaltjahr!\n", out.toString());
  104. }
  105. @Test
  106. public void testNumber() {
  107. int expected = 0;
  108. int actual = getNumber();
  109. assertEquals(expected, actual);
  110. }
  111. private int getNumber() {
  112. return 0;
  113. }
  114. @Test
  115. public void testTimer() {
  116. int actualTime = (int) (Math.random() * 10 + 1);
  117. int estimatedTime = (int) (Math.random() * 10 + 1);
  118. int difference = Math.abs(actualTime - estimatedTime);
  119. difference--;
  120. boolean result = true;
  121. result = true;
  122. assertTrue(result);
  123. }
  124. @Test
  125. void testtest() {
  126. assertTrue(true);
  127. }
  128. }