Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {expect, test} from "@jest/globals";
 
import {Graph} from "./Graph-demo.js";
 
 
test("Graph: constructor:", ()=>{
    const g = new Graph();
    g.addEdge(1, 2);
    g.addEdge(1, 3);
    g.addEdge(2, 3);
    g.addEdge(2, 4);
    const txt = g.toString();
    const expected = `1 → [2, 3]
2 → [1, 3, 4]
3 → [1, 2]
4 → [2]`;
    expect(txt).toBe(expected);
    const vertices = [1, 2, 3, 4];
    const action = (v) => expect(vertices).toContain(v);
    g.forEachVertices(action);
});
 
test("Graph: iterate all edges:", ()=>{
    const g = new Graph();
    g.addEdge(1, 2);
    g.addEdge(3, 1);
    g.addEdge(2, 3);
    g.addEdge(2, 4);
    const edges = [
        "1-2", "1-3", "2-3", "2-4"
    ];
    const action = (a, b) => {
        const [s,e] = a < b ? [a,b] : [b,a];
        const edge = `${s}-${e}`;
        expect(edges).toContain(edge)
    };
    g.forEachEdges(action);
});
 
test("Graph: Don't add floating point number to vertex set:", ()=>{
    const g = new Graph();
    try {
        g.addVertex(1.2);
    }catch (e) {
        expect(e.message).toBe('Argument 1.2 is not a valid vertex');
        expect(e.constructor).toBe(TypeError);
    }
});
 
test("Graph: Don't add string to vertex set:", ()=>{
    const g = new Graph();
    try {
        g.addVertex("add");
    }catch (e) {
        expect(e.message).toBe('Argument add is not a valid vertex');
        expect(e.constructor).toBe(TypeError);
    }
});
 
 
test("Graph: iterates over vertices:", ()=>{
    const g = new Graph();
    g.addEdge(1, 2);
    g.addEdge(1, 3);
    g.addEdge(2, 3);
    g.addEdge(2, 4);
    const vertices = [];
    for(const v of g.vertices()) {
        vertices.push(v);
    }
    vertices.sort();
    const expected = [1, 2, 3, 4];
    expect(vertices).toEqual(expected);
 
});
 
test("Graph: iterates over neighbor of a vertex:", ()=>{
    const g = new Graph();
    g.addEdge(1, 2);
    g.addEdge(1, 3);
    g.addEdge(2, 3);
    g.addEdge(2, 4);
    g.addEdge(4, 1);
    const vertices = [];
    for(const v of g.adjacentOf(1)) {
        vertices.push(v);
    }
    vertices.sort();
    const expected = [2, 3, 4];
    expect(vertices).toEqual(expected);
 
});
 
test("Graph: merge existing attributes:", ()=>{
    const g = new Graph();
    g.addVertex(1);
    const undef = g.getAttribute(1);
    expect(undef).toBe(undefined);
    const firstA = {'a':1, 'b':2};
    g.mergeAttribute(1, firstA);
    const getFirstA = g.getAttribute(1);
    expect(getFirstA.get('a')).toBe( firstA['a'] );
    expect(getFirstA.get('b')).toBe( firstA['b'] );
    const secondA = {'b': 3, 'c': 5};
    g.mergeAttribute(1,secondA);
    const getSecondA = g.getAttribute(1);
    expect(getSecondA.get('a')).toBe(firstA['a']); // same thing
    expect(getSecondA.get('b')).toBe(secondA['b']);
    expect(getSecondA.get('c')).toBe(secondA['c']);
 
});
 
test("Graph: don't merge attributes if given vertex not in graph:", ()=>{
    const g = new Graph();
    g.addVertex(1);
    try {
        g.mergeAttribute(2, {'a':1, 'b':2});
    }catch (e) {
        expect(e.message).toBe('Graph does not include vertex 2');
        expect(e.constructor).toBe(Error);
    }
});
 
test("Graph: don't merge bad attribute:", ()=>{
    const g = new Graph();
    g.addVertex(1);
    try {
        g.mergeAttribute(1, null);
    }catch (e) {
        expect(e.message).toBe('Bad attribute');
        expect(e.constructor).toBe(Error);
    }
});