Browse Source

testing pr

Nhu Nguyen 6 years ago
parent
commit
3222b88912
1 changed files with 50 additions and 0 deletions
  1. 50
    0
      src/test/java/org/zipcoder/store/TriangleTeset.java

+ 50
- 0
src/test/java/org/zipcoder/store/TriangleTeset.java View File

1
+package org.zipcoder.store;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class TriangleTeset {
7
+
8
+    @Test
9
+    public void one(){
10
+        Assert.assertEquals("*\n", triangle(1));
11
+    }
12
+
13
+    @Test
14
+    public void three(){
15
+        Assert.assertEquals(" *\n***\n", triangle(3));
16
+    }
17
+
18
+    @Test
19
+    public void five(){
20
+        Assert.assertEquals("  *\n ***\n*****\n", triangle(5));
21
+    }
22
+
23
+    @Test
24
+    public void seven(){
25
+        Assert.assertEquals("   *\n  ***\n *****\n*******\n", triangle(7));
26
+    }
27
+
28
+    private String triangle(int number) {
29
+        StringBuilder builder = new StringBuilder();
30
+
31
+        int star = 1;
32
+        int space = number/2;
33
+
34
+        while(star <= number) {
35
+            add(builder, ' ', space);
36
+            add(builder, '*', star);
37
+            builder.append('\n');
38
+            star += 2;
39
+            space -= 1;
40
+        }
41
+
42
+        return builder.toString();
43
+    }
44
+
45
+    private void add(StringBuilder builder, char value, int numberOfTimes) {
46
+        for (int i = 0; i < numberOfTimes; i++) {
47
+            builder.append(value);
48
+        }
49
+    }
50
+}