Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.stc; |
| 17 | |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 18 | import org.junit.AfterClass; |
| 19 | import org.junit.BeforeClass; |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.onlab.stc.MonitorLayout.Box; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.onlab.stc.CompilerTest.getStream; |
| 27 | import static org.onlab.stc.CompilerTest.stageTestResource; |
| 28 | import static org.onlab.stc.MonitorLayout.SLOT_WIDTH; |
| 29 | import static org.onlab.stc.Scenario.loadScenario; |
| 30 | |
| 31 | /** |
| 32 | * Tests of the monitor layout functionality. |
| 33 | */ |
| 34 | public class MonitorLayoutTest { |
| 35 | |
| 36 | private MonitorLayout layout; |
| 37 | |
HIGUCHI Yuta | d9c6117 | 2016-01-04 23:31:13 -0800 | [diff] [blame] | 38 | @BeforeClass |
| 39 | public static void setUpClass() throws IOException { |
| 40 | CompilerTest.setUpClass(); |
| 41 | } |
| 42 | |
| 43 | @AfterClass |
| 44 | public static void tearDownClass() throws IOException { |
| 45 | CompilerTest.tearDownClass(); |
| 46 | } |
| 47 | |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 48 | private Compiler getCompiler(String name) throws IOException { |
| 49 | stageTestResource(name); |
| 50 | Scenario scenario = loadScenario(getStream(name)); |
| 51 | Compiler compiler = new Compiler(scenario); |
| 52 | compiler.compile(); |
| 53 | return compiler; |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void basic() throws IOException { |
| 58 | layout = new MonitorLayout(getCompiler("layout-basic.xml")); |
| 59 | validate(layout, null, 0, 1, 5, 2); |
| 60 | validate(layout, "a", 1, 1, 1, 1, 1, -SLOT_WIDTH / 2); |
| 61 | validate(layout, "b", 2, 2, 1, 1, 0, 0); |
| 62 | validate(layout, "f", 3, 3, 1); |
| 63 | |
| 64 | validate(layout, "g", 1, 1, 4, 1, 1, SLOT_WIDTH / 2); |
| 65 | validate(layout, "c", 2, 1, 1); |
| 66 | validate(layout, "d", 3, 2, 1); |
| 67 | validate(layout, "e", 4, 3, 1); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void basicNest() throws IOException { |
| 72 | layout = new MonitorLayout(getCompiler("layout-basic-nest.xml")); |
| 73 | validate(layout, null, 0, 1, 6, 2); |
| 74 | validate(layout, "a", 1, 1, 1, 1, 1, -SLOT_WIDTH / 2); |
| 75 | validate(layout, "b", 2, 2, 1); |
| 76 | validate(layout, "f", 3, 3, 1); |
| 77 | |
| 78 | validate(layout, "g", 1, 1, 5, 1); |
| 79 | validate(layout, "c", 2, 1, 1); |
| 80 | |
| 81 | validate(layout, "gg", 3, 2, 3, 1); |
| 82 | validate(layout, "d", 4, 1, 1); |
| 83 | validate(layout, "e", 5, 2, 1); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void staggeredDependencies() throws IOException { |
| 88 | layout = new MonitorLayout(getCompiler("layout-staggered-dependencies.xml")); |
| 89 | validate(layout, null, 0, 1, 7, 4); |
| 90 | validate(layout, "a", 1, 1, 1, 1, 1, -SLOT_WIDTH - SLOT_WIDTH / 2); |
| 91 | validate(layout, "aa", 1, 1, 1, 1, 1, -SLOT_WIDTH / 2); |
| 92 | validate(layout, "b", 2, 2, 1); |
| 93 | validate(layout, "f", 3, 3, 1); |
| 94 | |
| 95 | validate(layout, "g", 1, 1, 5, 2, 1, +SLOT_WIDTH / 2); |
| 96 | validate(layout, "c", 2, 1, 1); |
| 97 | |
| 98 | validate(layout, "gg", 3, 2, 3, 2); |
| 99 | validate(layout, "d", 4, 1, 1); |
| 100 | validate(layout, "dd", 4, 1, 1); |
| 101 | validate(layout, "e", 5, 2, 1); |
| 102 | |
| 103 | validate(layout, "i", 6, 6, 1); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void deepNext() throws IOException { |
| 108 | layout = new MonitorLayout(getCompiler("layout-deep-nest.xml")); |
| 109 | validate(layout, null, 0, 1, 7, 6); |
| 110 | validate(layout, "a", 1, 1, 1); |
| 111 | validate(layout, "aa", 1, 1, 1); |
| 112 | validate(layout, "b", 2, 2, 1); |
| 113 | validate(layout, "f", 3, 3, 1); |
| 114 | |
| 115 | validate(layout, "g", 1, 1, 5, 2); |
| 116 | validate(layout, "c", 2, 1, 1); |
| 117 | |
| 118 | validate(layout, "gg", 3, 2, 3, 2); |
| 119 | validate(layout, "d", 4, 1, 1); |
| 120 | validate(layout, "dd", 4, 1, 1); |
| 121 | validate(layout, "e", 5, 2, 1); |
| 122 | |
| 123 | validate(layout, "i", 6, 6, 1); |
| 124 | |
| 125 | validate(layout, "g1", 1, 1, 6, 2); |
| 126 | validate(layout, "g2", 2, 1, 5, 2); |
| 127 | validate(layout, "g3", 3, 1, 4, 2); |
| 128 | validate(layout, "u", 4, 1, 1); |
| 129 | validate(layout, "v", 4, 1, 1); |
| 130 | validate(layout, "w", 5, 2, 1); |
| 131 | validate(layout, "z", 6, 3, 1); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | private void validate(MonitorLayout layout, String name, |
| 136 | int absoluteTier, int tier, int depth, int breadth) { |
| 137 | Box b = layout.get(name); |
| 138 | assertEquals("incorrect absolute tier", absoluteTier, b.absoluteTier()); |
| 139 | assertEquals("incorrect tier", tier, b.tier()); |
| 140 | assertEquals("incorrect depth", depth, b.depth()); |
| 141 | assertEquals("incorrect breadth", breadth, b.breadth()); |
| 142 | } |
| 143 | |
| 144 | private void validate(MonitorLayout layout, String name, |
| 145 | int absoluteTier, int tier, int depth, int breadth, |
| 146 | int top, int center) { |
| 147 | validate(layout, name, absoluteTier, tier, depth, breadth); |
| 148 | Box b = layout.get(name); |
| 149 | assertEquals("incorrect top", top, b.top()); |
| 150 | assertEquals("incorrect center", center, b.center()); |
| 151 | } |
| 152 | |
| 153 | private void validate(MonitorLayout layout, String name, |
| 154 | int absoluteTier, int tier, int depth) { |
| 155 | validate(layout, name, absoluteTier, tier, depth, 1); |
| 156 | } |
| 157 | |
| 158 | } |