<commit_before>#! /usr/bin/env python
''' unit test (suite) module checks functionality of all other programmed modules '''

import unittest
import mult_weights_solver as mwsolv
from highdimgraph import HighDimLine, HighDimLineSegment, has_crossing 
from highdimgraph import np_assert_allclose, create_line, create_linesegment
from highdimgraph import create_graph
import highdimgraph
import sariel_lp_solver as slpsolv
import fekete_lp_solver as flpsolv
import opt_solver
import math
import numpy as np

class CrossingTestCase(unittest.TestCase):
    def test_has_crossing(self):
        line = HighDimLine(np.array([(3., 2), (5., 0)]))  # y = -x + 5
        line_segment = HighDimLineSegment(np.array([(3, 0.5), (5, 1.5)]))  # y = 0.5 x - 1
        # line_points[0] = segpoints[0] = intersection point
        line_points = np.array([(4., 1.), (5., 0.)])
        self.assertTrue(line.is_on(line_points[0]))
        self.assertTrue(line.is_on(line_points[1]))
        
        seg_points = np.copy(line_points)
        seg_points[1, 1] = 1.5
        self.assertTrue(line_segment.is_on(seg_points[0]))
        self.assertTrue(line_segment.is_on(seg_points[1]))
        
        
        line_val = line.call(np.array([5.]))
        np_assert_allclose(0.0, line_val)
        np_assert_allclose(line_points[..., -1], line(line_points[..., :-1]))
        # np_assert_allclose(seg_points[..., -1], line_segment(seg_points[..., :-1]))
        self.assertTrue(has_crossing(line, line_segment))

    def test_has_no_crossing_linesegment_too_short(self):
        line = HighDimLine(np.array([[3., 2], [5, 0]]))  # y = -x + 5
        line_segment = HighDimLineSegment(np.array([[0., -1], [3, 0.5]]))  # y = 0.5 x - 1
        self.assertFalse(has_crossing(line, line_segment))

    def test_has_no_crossing_line_and_segment_parallel(self):
        line = HighDimLine(np.array([(3, 2), (5, 0)]))  # y = -x + 5
        line_segment = HighDimLineSegment(np.array([(0, 4), (6, -2)]))  # y = -x + 4
        self.assertFalse(has_crossing(line, line_segment))

    def test_has_also_crossing(self):
        line = HighDimLine(np.array([(3., 5.5), (5., 6.5)]))  # y = 0.5x + 4
        line_segment = HighDimLineSegment(np.array([(5., 7.), (6., 4.)]))  # y = -3x + 22
        self.assertTrue(has_crossing(line, line_segment))

class PreprocessingLinesTestCase(unittest.TestCase):
    def test_is_above_works(self):
        line = HighDimLine(np.array(((3, 2), (5, 0))))  # y = -x + 5
        p = np.array((1, 4.5))
        self.assertTrue(line.is_above(p))
        self.assertFalse(line.is_below(p))

    def test_is_below_works(self):
        line = HighDimLine(np.array(((3, 0.5), (5, 1.5))))  # y = 0.5 x - 1
        p = np.array((4., -1))
        self.assertTrue(line.is_below(p))
        self.assertFalse(line.is_above(p))

    def test_is_on_works(self):
        line = HighDimLine(np.array(((3, 0.5), (5, 1.5))))  # y = 0.5 x - 1
        p = np.array((4, 1))
        self.assertTrue(line.is_on(p))
        self.assertFalse(line.is_below(p))
        self.assertFalse(line.is_above(p))

    def test_preprocessing_lines_omits_duplicates(self):
        points = np.array([(2., 2.), (6., 4.), (3., 6.), (5., 7.), (4.25, 5.)])
        graph = create_graph(points, 5, 2)
        l1 = create_line(np.array((2., 6.)), np.array((3., 2.)))  # y = -4x + 14
        l2 = create_line(np.array((2., 3.)), np.array((6., 5.)))  # y = 0.5x + 2
        l3 = create_line(np.array((3., 5.5)), np.array((5., 6.5)))  # y = 0.5x + 4
        # duplicates part
        l4 = HighDimLine(np.array(((2.5, 6.), (3.5, 2.))))  # y = -4x + 16
        l5 = HighDimLine(np.array(((2., 2.5), (6., 4.5))))  # y = 0.5x + 1.5
        l6 = HighDimLine(np.array(((3., 5.), (5., 6.))))  # y = 0.5x + 3.5
        # lines outside of point set
        # above
        l7 = create_line(np.array((0., 7.)), np.array((1., 10.)))  # y = 3 x + 7
        # below
        l8 = create_line(np.array((0., -1.)), np.array((6., 1.)))  # y = 1/3 x - 1
        # line between points omit it
        l9 = create_line(np.array((3., 6.)), np.array((5., 7.)))
        lines = set([l1, l2, l3, l4, l5, l6, l7, l8, l9])
        graph.lines = lines
        graph.preprocess_lines()
        result = graph.get_lines()
        self.assertEqual(len(result), 3)
        self.assertFalse(l7 in result)
        self.assertFalse(l8 in result)
        self.assertFalse(l9 in result)
        self.assertTrue(l1 in result or l4 in result)
        self.assertTrue(l2 in result or l5 in result)
        self.assertTrue(l3 in result or l6 in result)


class GraphTestCase(unittest.TestCase):
    def setUp(self):
        point_set = np.array([[0], [1], [2], [3]])
        self.graph = create_graph(point_set, 4, 1)

    def test_connected_components(self):
        cc1 = self.graph.connected_components.get_connected_component(1)
        cc2 = self.graph.connected_components.get_connected_component(2)
        self.assertFalse(cc1 == cc2)
        cc3 = self.graph.connected_components.get_connected_component(1)
        self.assertTrue(cc1 == cc3)

    def test_adjacent(self):
        self.assertTrue(self.graph.edges.has_edge(1,2))
        self.assertFalse(self.graph.edges.has_edge(1,5))
        adjacent_vertices = list(self.graph.edges.get_adj_nodes(1))
        adjacent_vertices.sort()
        self.assertEqual(adjacent_vertices, [0,2,3])

    def test_get_edges(self):
        edges = []
        for (i,j) in self.graph.edges:
            edges.append((i,j))
        expected_edges = [(0,1),(0,2),(0,3), (1,2), (1,3), (2,3)]
        self.assertEqual(edges, expected_edges)

    def test_merge_connected_components(self):<commit_msg>graphtest case up to date for graph impl<commit_after>#! /usr/bin/env python
''' unit test (suite) module checks functionality of all other programmed modules '''

import unittest
import mult_weights_solver as mwsolv
from highdimgraph import HighDimLine, HighDimLineSegment, has_crossing 
from highdimgraph import np_assert_allclose, create_line, create_linesegment
from highdimgraph import create_graph
import highdimgraph
import sariel_lp_solver as slpsolv
import fekete_lp_solver as flpsolv
import opt_solver
import math
import numpy as np

class CrossingTestCase(unittest.TestCase):
    def test_has_crossing(self):
        line = HighDimLine(np.array([(3., 2), (5., 0)]))  # y = -x + 5
        line_segment = HighDimLineSegment(np.array([(3, 0.5), (5, 1.5)]))  # y = 0.5 x - 1
        # line_points[0] = segpoints[0] = intersection point
        line_points = np.array([(4., 1.), (5., 0.)])
        self.assertTrue(line.is_on(line_points[0]))
        self.assertTrue(line.is_on(line_points[1]))
        
        seg_points = np.copy(line_points)
        seg_points[1, 1] = 1.5
        self.assertTrue(line_segment.is_on(seg_points[0]))
        self.assertTrue(line_segment.is_on(seg_points[1]))
        
        
        line_val = line.call(np.array([5.]))
        np_assert_allclose(0.0, line_val)
        np_assert_allclose(line_points[..., -1], line(line_points[..., :-1]))
        # np_assert_allclose(seg_points[..., -1], line_segment(seg_points[..., :-1]))
        self.assertTrue(has_crossing(line, line_segment))

    def test_has_no_crossing_linesegment_too_short(self):
        line = HighDimLine(np.array([[3., 2], [5, 0]]))  # y = -x + 5
        line_segment = HighDimLineSegment(np.array([[0., -1], [3, 0.5]]))  # y = 0.5 x - 1
        self.assertFalse(has_crossing(line, line_segment))

    def test_has_no_crossing_line_and_segment_parallel(self):
        line = HighDimLine(np.array([(3, 2), (5, 0)]))  # y = -x + 5
        line_segment = HighDimLineSegment(np.array([(0, 4), (6, -2)]))  # y = -x + 4
        self.assertFalse(has_crossing(line, line_segment))

    def test_has_also_crossing(self):
        line = HighDimLine(np.array([(3., 5.5), (5., 6.5)]))  # y = 0.5x + 4
        line_segment = HighDimLineSegment(np.array([(5., 7.), (6., 4.)]))  # y = -3x + 22
        self.assertTrue(has_crossing(line, line_segment))

class PreprocessingLinesTestCase(unittest.TestCase):
    def test_is_above_works(self):
        line = HighDimLine(np.array(((3, 2), (5, 0))))  # y = -x + 5
        p = np.array((1, 4.5))
        self.assertTrue(line.is_above(p))
        self.assertFalse(line.is_below(p))

    def test_is_below_works(self):
        line = HighDimLine(np.array(((3, 0.5), (5, 1.5))))  # y = 0.5 x - 1
        p = np.array((4., -1))
        self.assertTrue(line.is_below(p))
        self.assertFalse(line.is_above(p))

    def test_is_on_works(self):
        line = HighDimLine(np.array(((3, 0.5), (5, 1.5))))  # y = 0.5 x - 1
        p = np.array((4, 1))
        self.assertTrue(line.is_on(p))
        self.assertFalse(line.is_below(p))
        self.assertFalse(line.is_above(p))

    def test_preprocessing_lines_omits_duplicates(self):
        points = np.array([(2., 2.), (6., 4.), (3., 6.), (5., 7.), (4.25, 5.)])
        graph = create_graph(points, 5, 2)
        l1 = create_line(np.array((2., 6.)), np.array((3., 2.)))  # y = -4x + 14
        l2 = create_line(np.array((2., 3.)), np.array((6., 5.)))  # y = 0.5x + 2
        l3 = create_line(np.array((3., 5.5)), np.array((5., 6.5)))  # y = 0.5x + 4
        # duplicates part
        l4 = HighDimLine(np.array(((2.5, 6.), (3.5, 2.))))  # y = -4x + 16
        l5 = HighDimLine(np.array(((2., 2.5), (6., 4.5))))  # y = 0.5x + 1.5
        l6 = HighDimLine(np.array(((3., 5.), (5., 6.))))  # y = 0.5x + 3.5
        # lines outside of point set
        # above
        l7 = create_line(np.array((0., 7.)), np.array((1., 10.)))  # y = 3 x + 7
        # below
        l8 = create_line(np.array((0., -1.)), np.array((6., 1.)))  # y = 1/3 x - 1
        # line between points omit it
        l9 = create_line(np.array((3., 6.)), np.array((5., 7.)))
        lines = set([l1, l2, l3, l4, l5, l6, l7, l8, l9])
        graph.lines = lines
        graph.preprocess_lines()
        result = graph.get_lines()
        self.assertEqual(len(result), 3)
        self.assertFalse(l7 in result)
        self.assertFalse(l8 in result)
        self.assertFalse(l9 in result)
        self.assertTrue(l1 in result or l4 in result)
        self.assertTrue(l2 in result or l5 in result)
        self.assertTrue(l3 in result or l6 in result)


class GraphTestCase(unittest.TestCase):
    def setUp(self):
        point_set = np.array([[0], [1], [2], [3]])
        self.graph = create_graph(point_set, 4, 1)

    def test_connected_components(self):
        cc1 = self.graph.connected_components.get_connected_component(1)
        cc2 = self.graph.connected_components.get_connected_component(2)
        self.assertFalse(cc1 == cc2)
        cc3 = self.graph.connected_components.get_connected_component(1)
        self.assertTrue(cc1 == cc3)

    def test_adjacent(self):
        self.assertTrue(self.graph.edges.has_edge(1,2))
        self.assertFalse(self.graph.edges.has_edge(1,1))
        adjacent_vertices = list(self.graph.edges.adj_nodes(1))
        adjacent_vertices.sort()
        self.assertEqual(adjacent_vertices, [0,2,3])

    def test_get_edges(self):
        edges = []
        for (i,j) in self.graph.edges:
            edges.append((i,j))
        expected_edges = [(0,1),(0,2),(0,3), (1,2), (1,3), (2,3)]
        self.assertEqual(edges, expected_edges)

    def test_merge_connected_components(self):
{'fraction_non_alphanumeric': 0.16706401766004414, 'fraction_numerical': 0.04415011037527594, 'mean_word_length': 2.718319107025607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '301339', 'n_tokens_mistral': 4329, 'n_tokens_neox': 4118, 'n_words': 842}