diff --git a/Charts.xcodeproj/project.pbxproj b/Charts.xcodeproj/project.pbxproj index 01b235cb0a..1a61360a76 100644 --- a/Charts.xcodeproj/project.pbxproj +++ b/Charts.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ 17E994DA88777AA1D8CCFC58 /* BarChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = C31AA65EA27776F8C653C7E8 /* BarChartDataSet.swift */; }; 203A39685CC96FC625F616E4 /* IHighlighter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 998F2BFE318471AFC05B50AC /* IHighlighter.swift */; }; 219192CA6B4895319AB49DCA /* BarLineScatterCandleBubbleRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B1C588E9DF6FFD56D7ADF8E /* BarLineScatterCandleBubbleRenderer.swift */; }; + 224EFF991FBAAC4700CF9B3B /* EquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 224EFF981FBAAC4700CF9B3B /* EquatableTests.swift */; }; 23649EFC635A76022F07FFA6 /* PieChartDataEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD02157CF8CEE1189BF681DA /* PieChartDataEntry.swift */; }; 23FA50B2730D8C7ACA091C4F /* BarChartRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75F279974FE650E57A061B09 /* BarChartRenderer.swift */; }; 24151B0729D77251A8494D70 /* LineRadarRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 105FFC9D3773A9C7A60A897F /* LineRadarRenderer.swift */; }; @@ -182,6 +183,7 @@ 1F3D55A7E6176D52DC957D27 /* XAxisRendererHorizontalBarChart.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = XAxisRendererHorizontalBarChart.swift; path = Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift; sourceTree = ""; }; 2194AA554712E6BA2677F114 /* BubbleChartRenderer.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = BubbleChartRenderer.swift; path = Source/Charts/Renderers/BubbleChartRenderer.swift; sourceTree = ""; }; 219BC9CEA037F897E92E45D1 /* IScatterChartDataSet.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = IScatterChartDataSet.swift; path = Source/Charts/Data/Interfaces/IScatterChartDataSet.swift; sourceTree = ""; }; + 224EFF981FBAAC4700CF9B3B /* EquatableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = EquatableTests.swift; path = Tests/Charts/EquatableTests.swift; sourceTree = ""; }; 23D35CF6F9177D77B6B97AE1 /* XShapeRenderer.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = XShapeRenderer.swift; path = Source/Charts/Renderers/Scatter/XShapeRenderer.swift; sourceTree = ""; }; 2440DB759AB93B4A928A3F6F /* RadarChartView.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = RadarChartView.swift; path = Source/Charts/Charts/RadarChartView.swift; sourceTree = ""; }; 2465CB73738EBAFB46C57288 /* CombinedHighlighter.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = CombinedHighlighter.swift; path = Source/Charts/Highlight/CombinedHighlighter.swift; sourceTree = ""; }; @@ -510,6 +512,7 @@ isa = PBXGroup; children = ( 5C3F5E1A69EC06E86505F7B1 /* BarChartTests.swift */, + 224EFF981FBAAC4700CF9B3B /* EquatableTests.swift */, 7AB9062A28AAB9469752A954 /* ChartUtilsTests.swift */, D2E1819D72CD7B6C4A4E8048 /* LineChartTests.swift */, 064989451F5C99C7006E8BB3 /* Snapshot.swift */, @@ -945,6 +948,7 @@ 8E1192F7A7152E9DA92C56A9 /* ChartUtilsTests.swift in Sources */, 2BF85BEA981B359A65E9BF67 /* LineChartTests.swift in Sources */, 064989461F5C99C7006E8BB3 /* Snapshot.swift in Sources */, + 224EFF991FBAAC4700CF9B3B /* EquatableTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index 1f07a19084..5c11209d50 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -530,7 +530,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD let h = getHighlightByTouchPoint(recognizer.location(in: self)) - if h?.isEqual(lastHighlighted) ?? true + if h === nil || h == self.lastHighlighted { highlightValue(nil, callDelegate: true) lastHighlighted = nil @@ -748,9 +748,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD let lastHighlighted = self.lastHighlighted - if (h === nil && lastHighlighted !== nil) || - (h !== nil && lastHighlighted === nil) || - (lastHighlighted !== nil && !(h?.isEqual(lastHighlighted) ?? true)) + if h != lastHighlighted { self.lastHighlighted = h self.highlightValue(h, callDelegate: true) diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataEntry.swift b/Source/Charts/Data/Implementations/Standard/ChartDataEntry.swift index b7edc6ddef..9021c56670 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataEntry.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataEntry.swift @@ -69,24 +69,7 @@ open class ChartDataEntry: ChartDataEntryBase self.x = x } - - // MARK: NSObject - - open override func isEqual(_ object: Any?) -> Bool - { - if !super.isEqual(object) - { - return false - } - if fabs((object! as AnyObject).x - x) > Double.ulpOfOne - { - return false - } - - return true - } - // MARK: NSObject open override var description: String @@ -108,32 +91,18 @@ open class ChartDataEntry: ChartDataEntryBase } } -public func ==(lhs: ChartDataEntry, rhs: ChartDataEntry) -> Bool -{ - if lhs === rhs - { - return true - } - - if !lhs.isKind(of: type(of: rhs)) - { - return false - } - - if lhs.data !== rhs.data && !lhs.data!.isEqual(rhs.data) - { - return false - } - - if fabs(lhs.x - rhs.x) > Double.ulpOfOne - { - return false - } - - if fabs(lhs.y - rhs.y) > Double.ulpOfOne - { - return false +// MARK: Equatable +extension ChartDataEntry/*: Equatable*/ { + open override func isEqual(_ object: Any?) -> Bool { + guard let object = object as? ChartDataEntry else { return false } + + if self === object + { + return true + } + + return ((data == nil && object.data == nil) || (data?.isEqual(object.data) ?? false)) + && y == object.y + && x == object.x } - - return true } diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataEntryBase.swift b/Source/Charts/Data/Implementations/Standard/ChartDataEntryBase.swift index 6dd67ca12c..0dcb1c71c6 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataEntryBase.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataEntryBase.swift @@ -70,34 +70,7 @@ open class ChartDataEntryBase: NSObject self.icon = icon self.data = data } - - // MARK: NSObject - - open override func isEqual(_ object: Any?) -> Bool - { - if object == nil - { - return false - } - - if !(object! as AnyObject).isKind(of: type(of: self)) - { - return false - } - - if (object! as AnyObject).data !== data && !((object! as AnyObject).data??.isEqual(self.data))! - { - return false - } - - if fabs((object! as AnyObject).y - y) > Double.ulpOfOne - { - return false - } - - return true - } - + // MARK: NSObject open override var description: String @@ -106,27 +79,17 @@ open class ChartDataEntryBase: NSObject } } -public func ==(lhs: ChartDataEntryBase, rhs: ChartDataEntryBase) -> Bool -{ - if lhs === rhs - { - return true - } - - if !lhs.isKind(of: type(of: rhs)) - { - return false - } - - if lhs.data !== rhs.data && !lhs.data!.isEqual(rhs.data) - { - return false - } - - if fabs(lhs.y - rhs.y) > Double.ulpOfOne - { - return false +// MARK: Equatable +extension ChartDataEntryBase/*: Equatable*/ { + open override func isEqual(_ object: Any?) -> Bool { + guard let object = object as? ChartDataEntryBase else { return false } + + if self === object + { + return true + } + + return ((data == nil && object.data == nil) || (data?.isEqual(object.data) ?? false)) + && y == object.y } - - return true } diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index 05532b8a82..716008fdd7 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -463,7 +463,7 @@ open class ChartDataSet: ChartBaseDataSet { for entry in values { - if (entry.isEqual(e)) + if entry == e { return true } diff --git a/Source/Charts/Highlight/Highlight.swift b/Source/Charts/Highlight/Highlight.swift index a33127b8ba..5c3c9df3a4 100644 --- a/Source/Charts/Highlight/Highlight.swift +++ b/Source/Charts/Highlight/Highlight.swift @@ -174,84 +174,24 @@ open class Highlight: NSObject { return "Highlight, x: \(_x), y: \(_y), dataIndex (combined charts): \(dataIndex), dataSetIndex: \(_dataSetIndex), stackIndex (only stacked barentry): \(_stackIndex)" } - - open override func isEqual(_ object: Any?) -> Bool - { - if object == nil - { - return false - } - - if !(object! as AnyObject).isKind(of: type(of: self)) - { - return false - } - - if (object! as AnyObject).x != _x - { - return false - } - - if (object! as AnyObject).y != _y - { - return false - } - - if (object! as AnyObject).dataIndex != dataIndex - { - return false - } - - if (object! as AnyObject).dataSetIndex != _dataSetIndex - { - return false - } - - if (object! as AnyObject).stackIndex != _stackIndex +} + + +// MARK: Equatable +extension Highlight /*: Equatable*/ { + open override func isEqual(_ object: Any?) -> Bool { + guard let object = object as? Highlight else { return false } + + if self === object { - return false + return true } - - return true - } -} -func ==(lhs: Highlight, rhs: Highlight) -> Bool -{ - if lhs === rhs - { - return true - } - - if !lhs.isKind(of: type(of: rhs)) - { - return false - } - - if lhs._x != rhs._x - { - return false - } - - if lhs._y != rhs._y - { - return false - } - - if lhs.dataIndex != rhs.dataIndex - { - return false - } - - if lhs._dataSetIndex != rhs._dataSetIndex - { - return false - } - - if lhs._stackIndex != rhs._stackIndex - { - return false + return _x == object._x + && _y == object._y + && dataIndex == object.dataIndex + && _dataSetIndex == object._dataSetIndex + && _stackIndex == object._stackIndex + } - - return true } diff --git a/Tests/Charts/EquatableTests.swift b/Tests/Charts/EquatableTests.swift new file mode 100644 index 0000000000..3d481776dd --- /dev/null +++ b/Tests/Charts/EquatableTests.swift @@ -0,0 +1,45 @@ +// +// EquatableTests.swift +// Charts +// +// Created by Jacob Christie on 2017-11-13. +// + +import XCTest +@testable import Charts + +class EquatableTests: XCTestCase { + func testChartDataEntryEquality() { + let image = UIImage() + let data = NSObject() + let entry1 = ChartDataEntry(x: 5, y: 3, icon: image, data: data) + let entry2 = ChartDataEntry(x: 5, y: 3, icon: image, data: data) + + XCTAssertTrue(entry1 == entry2) + } + + func testChartDataEntryInequality() { + let image = UIImage() + let data1 = NSObject() + let data2 = NSObject() + let entry1 = ChartDataEntry(x: 5, y: 3, icon: image, data: data1) + let entry2 = ChartDataEntry(x: 5, y: 3, icon: image, data: data2) + + XCTAssertFalse(entry1 == entry2) + } + + func testHighlightEquality() { + let high1 = Highlight(x: 5, y: 3, xPx: 1, yPx: -1, dataSetIndex: 8, stackIndex: 8, axis: .right) + let high2 = Highlight(x: 5, y: 3, xPx: 1, yPx: -1, dataSetIndex: 8, stackIndex: 8, axis: .right) + + XCTAssertTrue(high1 == high2) + } + + func testHighlightInequality() { + let high1 = Highlight(x: 5, y: 3, xPx: 1, yPx: -1, dataSetIndex: 8, stackIndex: 8, axis: .left) + let high2 = Highlight(x: 5, y: 3, xPx: 1, yPx: -1, dataSetIndex: 8, stackIndex: 9, axis: .left) + + XCTAssertFalse(high1 == high2) + } +} +