aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-graph/src/test/java/org/sonar/graph/DsmTest.java
blob: c070457ee0cc17608f922cf1c5bbd7d51d468f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2011 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.graph;

import org.junit.Before;
import org.junit.Test;
import org.sonar.graph.Dsm;
import org.sonar.graph.DsmScanner;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class DsmTest {

  private Dsm<String> dsm;

  @Before
  public void init() {
    StringPrintWriter textDsm = new StringPrintWriter();
    textDsm.println("  | A | B | C | D | E |");
    textDsm.println("A |   | 1 |   |   |   |");
    textDsm.println("B | 2 |   |   |   |   |");
    textDsm.println("C | 4 |   |   |   |   |");
    textDsm.println("D |   | 7 |   |   | 5 |");
    textDsm.println("E |   |   |   |   |   |");
    dsm = DsmScanner.scan(textDsm.toString());
  }

  @Test
  public void testGetVertex() {
    assertEquals("A", dsm.getVertex(0));
    assertEquals("B", dsm.getVertex(1));
    assertEquals("C", dsm.getVertex(2));
    assertEquals("D", dsm.getVertex(3));
    assertEquals("E", dsm.getVertex(4));
  }

  @Test
  public void testPermute() {
    assertEquals(2, dsm.getCell(0, 1).getWeight());
    assertEquals(4, dsm.getCell(0, 2).getWeight());

    dsm.permute(0, 1);
    assertEquals(1, dsm.getCell(0, 1).getWeight());
    assertEquals(0, dsm.getCell(0, 2).getWeight());
  }

  @Test
  public void testGetNumberOfOutgoingEdges() {
    assertEquals(0, dsm.getNumberOfOutgoingEdges(3, 0, 4));
    assertEquals(2, dsm.getNumberOfOutgoingEdges(0, 0, 4));
  }

  @Test
  public void testGetNumberOfIncomingEdges() {
    assertThat(dsm.getNumberOfIncomingEdges(0, 0, 4), equalTo(1));
    assertThat(dsm.getNumberOfIncomingEdges(4, 0, 4), equalTo(0));
  }
}