Advertisement
brentfisher-72

DFS Graph traversal Test IK Solution Correction T

Apr 13th, 2024
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | Source Code | 0 0
  1. package com.brent.ik.graphs;
  2.  
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.junit.jupiter.api.Test;
  6.  
  7. import java.util.List;
  8.  
  9. import static com.brent.ik.graphs.DFS.dfs_traversal;
  10. import static org.assertj.core.api.Assertions.assertThat;
  11.  
  12.  
  13. class DFSTest {
  14.  
  15.     public static class GraphData {
  16.         private int n;
  17.         private List<List<Integer>> edges;
  18.  
  19.         public int getN() {
  20.             return n;
  21.         }
  22.  
  23.         public void setN(int n) {
  24.             this.n = n;
  25.         }
  26.  
  27.         public List<List<Integer>> getEdges() {
  28.             return edges;
  29.         }
  30.  
  31.         public void setEdges(List<List<Integer>> edges) {
  32.             this.edges = edges;
  33.         }
  34.     }
  35.  
  36.     @Test
  37.     void shouldTraverseGraph_givenGraph() throws Exception {
  38.         var inputJson = """
  39.                 {
  40.                     "n": 6,
  41.                     "edges": [
  42.                     [0, 1],
  43.                     [0, 2],
  44.                     [0, 4],
  45.                     [2, 3]
  46.                     ]
  47.                 }
  48. """;
  49.         var expectedString = """
  50.                 [0, 4, 2, 3, 1, 5]
  51. """;       
  52.         var json = new ObjectMapper();
  53.         GraphData graphData = json.readValue(inputJson,GraphData.class);
  54.        
  55.         List<Integer> expected = json.readValue(expectedString, new TypeReference<List<Integer>>(){});
  56.         var actual = dfs_traversal(graphData.n,graphData.edges);
  57.         assertThat(actual).isEqualTo(expected);
  58.        
  59.     }
  60. }
  61. /**
  62. <T> T readValue(String content,
  63.                        com.fasterxml.jackson.core.type.TypeReference<T> valueTypeRef)
  64. */
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement