Skip to content

Commit

Permalink
Merge pull request #32 from ricglz0201/orOperator
Browse files Browse the repository at this point in the history
Implement OR, XOR and NOR operations
  • Loading branch information
JALVITO authored May 5, 2020
2 parents 2a0f0bc + ab2062e commit a9d41e4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
9 changes: 9 additions & 0 deletions BaseCalc/EnvironmentalObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ enum Operation: String {
case multiply = "x";
case divide = "÷";
case and = "AND";
case or = "OR";
case xor = "XOR";
case nor = "NOR";
}

class LayoutState: ObservableObject {
Expand Down Expand Up @@ -124,6 +127,12 @@ class CalculatorState: ObservableObject {
changePrevNumber(answer: (prevNumber ?? currentNumber) - currentNumber)
case .and:
changePrevNumber(answer: (prevNumber ?? currentNumber) & currentNumber)
case .or:
changePrevNumber(answer: (prevNumber ?? currentNumber) | currentNumber)
case .xor:
changePrevNumber(answer: (prevNumber ?? currentNumber) ^ currentNumber)
case .nor:
changePrevNumber(answer: (prevNumber ?? currentNumber) ~| currentNumber)
default:
print(prevOperation == nil)
}
Expand Down
30 changes: 30 additions & 0 deletions BaseCalc/Number.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class FloatingPoint: NSObject {
}
}

infix operator ~| : AdditionPrecedence

class Number: NSObject {
var value: Double
var base: Base
Expand Down Expand Up @@ -209,4 +211,32 @@ class Number: NSObject {

return Number(number: result, base: rightNum.base)
}

static func | (leftNum: Number, rightNum: Number) -> Number {
let leftVal = Int(leftNum.value)
let rightVal = Int(rightNum.value)
let result = String(leftVal | rightVal)

return Number(number: result, base: rightNum.base)
}

static func ^ (leftNum: Number, rightNum: Number) -> Number {
let leftVal = Int(leftNum.value)
let rightVal = Int(rightNum.value)
let result = String(leftVal ^ rightVal)

return Number(number: result, base: rightNum.base)
}

static func ~| (leftNum: Number, rightNum: Number) -> Number {
let orResult = leftNum | rightNum

let ogBase = orResult.base
orResult.setBase(base: .Base2)

let norResult = orResult.radixComplementDiminished()
norResult.setBase(base: ogBase)

return norResult
}
}
4 changes: 2 additions & 2 deletions BaseCalc/Views/KeypadButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ struct KeypadButton: View {
Text(label)
.modifier(LightGrayButton(width: width, height: height, altCondition: false))
})
case "AND":
case "AND", "OR", "XOR", "NOR":
return makeBitwiseButton(op: Operation(rawValue: label)!)
case "NOR", "OR", "XOR", ">>", "<<", "X>>Y", "X<<Y":
case ">>", "<<", "X>>Y", "X<<Y":
let disabled = calculatorState.isInvalidForBitOperations()
return AnyView(Button(action: generalAction({})) {
Text(label)
Expand Down
15 changes: 15 additions & 0 deletions BaseCalcTests/CalculatorStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ class CalculatorStateTests: XCTestCase {
numericalOperationsAux(and, "AND")
}

func testOr() {
let or = { self.state.performOperation(op: .or) }
numericalOperationsAux(or, "OR")
}

func testXor() {
let xor = { self.state.performOperation(op: .xor) }
numericalOperationsAux(xor, "XOR")
}

func testNor() {
let nor = { self.state.performOperation(op: .nor) }
numericalOperationsAux(nor, "NOR")
}

//MARK:- Change Sign Operation

func testChangeSignOfZero() {
Expand Down
27 changes: 27 additions & 0 deletions BaseCalcTests/NumberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,31 @@ class NumberTests: XCTestCase {
XCTAssertFalse(num3.hasFract)
XCTAssertEqual(num3.base, Base.Base10)
}

func testOrOperation(){
num1 = Number(number: "10", base: .Base10)
num2 = Number(number: "3", base: .Base10)
num3 = num1 | num2
XCTAssertEqual(num3.value, 11.0)
XCTAssertFalse(num3.hasFract)
XCTAssertEqual(num3.base, Base.Base10)
}

func testXorOperation(){
num1 = Number(number: "10", base: .Base10)
num2 = Number(number: "3", base: .Base10)
num3 = num1 ^ num2
XCTAssertEqual(num3.value, 9.0)
XCTAssertFalse(num3.hasFract)
XCTAssertEqual(num3.base, Base.Base10)
}

func testNorOperation(){
num1 = Number(number: "10", base: .Base10)
num2 = Number(number: "3", base: .Base10)
num3 = num1 ~| num2
XCTAssertEqual(num3.value, 4.0)
XCTAssertFalse(num3.hasFract)
XCTAssertEqual(num3.base, Base.Base10)
}
}

0 comments on commit a9d41e4

Please sign in to comment.