Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GradeType tests #35

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ struct gradeTableCell: View {

for i in 0...3 {
let arr = tests.filter({$0.year == i+1});
if(arr.count == 0){continue}
if(arr.count == 0){ continue }

if(!Util.checkinactiveYears(Util.getinactiveYears(subject), i+1)){continue}
if(!Util.checkinactiveYears(Util.getinactiveYears(subject), i+1)){ continue }
let points = Int(round(Util.testAverage(arr)))

str[i] = String(points)
Expand Down
4 changes: 2 additions & 2 deletions Calq/Screens/SubjectList/SubjectListVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class SubjectListVM: ObservableObject {
}

func calcInactiveYearsCount()-> Int{
if(subjects.count == 0) {return 0}
if(subjects.count == 0) { return 0 }
var count: Int = 0

for sub in subjects {
let arr = Util.getinactiveYears(sub)
for num in arr {
if(num == "") {continue}
if(num == "") { continue }
if Int(num) != nil { count += 1}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Calq/lib/JSON/ExportJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension JSON {
for sub in subjects {
string += "{\"name\": \"\(sub.name)\", \"lk\": \(sub.lk), \"color\": \"\(sub.color)\", \"inactiveYears\": \"\(sub.inactiveYears )\", \"subjecttests\": ["

if(sub.subjecttests == nil){continue}
if(sub.subjecttests == nil){ continue }
let tests = sub.subjecttests!.allObjects as! [UserTest]
var testCount: Int = 0

Expand Down
74 changes: 35 additions & 39 deletions Calq/lib/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ struct Util {
}

static func isStringInputInvalid(_ str: String) -> Bool{
if(str.isEmpty){ return true }
if str.isEmpty { return true }
let regex = try! NSRegularExpression(pattern: "^[a-zA-Z_ ]*$")
let range = NSRange(location: 0, length: str.utf16.count)
return regex.firstMatch(in: str, options: [], range: range) == nil
}

// MARK: Average Functions
static func average (_ values: [Int]) -> Double {
if (values.count < 1) {return 0}
if values.count < 1 { return 0 }

var avg = 0
for i in 0..<values.count {
Expand All @@ -61,7 +61,7 @@ struct Util {
}

static private func average (_ values: [Double]) -> Double {
if (values.count < 1) {return 0}
if values.count < 1 { return 0 }

var avg = Double(0);
for i in 0..<values.count {
Expand All @@ -75,7 +75,7 @@ struct Util {
}

static private func average (_ values: [Double], from: Int = 0, to: Int = -1) -> Double {
if (from > values.count) {return 0;}
if (from > values.count) { return 0 }

var sum = Double(0);
if (to<0){for i in from..<values.count {
Expand Down Expand Up @@ -112,14 +112,14 @@ struct Util {
/// Returns the average of all grades from one subject
static func getSubjectAverage(_ sub: UserSubject) -> Double{
let tests = filterTests(sub)
if(tests.count == 0){return 0.0}
if tests.count == 0 { return 0.0 }

var count = 0.0
var subaverage = 0.0

for e in 1...4 {
let yearTests = tests.filter{$0.year == Int16(e)}
if(yearTests.count == 0) {continue}
if yearTests.count == 0 { continue }
count += 1
subaverage += Util.testAverage(yearTests)
}
Expand All @@ -130,26 +130,26 @@ struct Util {
/// Returns the average of all grades from one subject
static func getSubjectAverage(_ sub: UserSubject, year: Int, filterinactve: Bool = true) -> Double{
let tests = filterTests(sub, checkinactive: filterinactve).filter{$0.year == year};
if(tests.count == 0){return 0.0}
if tests.count == 0 { return 0.0 }
return testAverage(tests)
}

/// Returns the average of all grades from all subjects.
static func generalAverage() -> Double{
let allSubjects = getAllSubjects()

if(allSubjects.count == 0) { return 0.0}
if allSubjects.count == 0 { return 0.0 }
var a = 0.0
var subjectCount = Double(allSubjects.count)

for sub in allSubjects{
if(sub.subjecttests == nil){subjectCount-=1;continue}
if sub.subjecttests == nil { subjectCount-=1; continue }
let tests = filterTests(sub)
if(tests.count == 0){subjectCount-=1;continue}
if tests.count == 0 { subjectCount-=1; continue }
a += round(getSubjectAverage(sub))
}

if((a / subjectCount).isNaN) {return 0.0}
if (a / subjectCount).isNaN { return 0.0 }
return a / subjectCount
}

Expand All @@ -159,38 +159,38 @@ struct Util {
/// Returns the average of all grades from all subjects in a specific halfyear
static func generalAverage(_ year: Int) -> Double{
let allSubjects = getAllSubjects()
if(allSubjects.count == 0) { return 0.0}
if allSubjects.count == 0 { return 0.0 }
var count = 0.0;
var grades = 0.0;

for sub in allSubjects {
if(sub.subjecttests == nil){continue}
if(sub.subjecttests == nil){ continue }
let tests = filterTests(sub).filter{Int($0.year) == year}
if(tests.count == 0){continue}
if(tests.count == 0){ continue }
let multiplier = sub.lk ? 2.0 : 1.0

count += multiplier * 1
grades += multiplier * round(Util.testAverage(tests))
}
if(grades == 0.0){ return 0.0}
if grades == 0.0 { return 0.0 }
return grades / count
}

/// Converts the points(0-15) representation of a grade to the more common 1-6 scale.
static func grade(number: Double) -> Double {
if(number == 0.0){ return 0.0}
if number == 0.0 { return 0.0 }
return ((17 - abs(number)) / 3.0)
}

/// Generates a convient String that shows the grades of the subject.
static func averageString(_ subject: UserSubject) -> String{
var str: String = ""
if(subject.subjecttests == nil) {return str}
if subject.subjecttests == nil { return str }
let tests = subject.subjecttests!.allObjects as! [UserTest]

for i in 1...4 {
let arr = tests.filter({$0.year == i});
if(arr.count == 0) {str += "-- ";continue}
if arr.count == 0 { str += "-- "; continue }
str += String(Int(round(Util.testAverage(arr))))
if(i != 4){ str += " "}
}
Expand Down Expand Up @@ -267,7 +267,7 @@ struct Util {
var allSubjects: [UserSubject] = []
let settings = Util.getSettings()

if(settings.usersubjects != nil){
if settings.usersubjects != nil {
allSubjects = settings.usersubjects!.allObjects as! [UserSubject]
return sortSubjects(allSubjects)
}
Expand All @@ -285,15 +285,15 @@ struct Util {
static func getSubject(_ subject: UserSubject) -> UserSubject? {
let all = self.getAllSubjects()
let filtered = all.filter{$0.objectID == subject.objectID}
if (filtered.count < 1) {return nil}
if filtered.count < 1 { return nil }
return filtered[0]
}

/// Returns one Subject after ID
static func getSubject(_ id: NSManagedObjectID) -> UserSubject? {
let all = self.getAllSubjects()
let filtered = all.filter{$0.objectID == id}
if (filtered.count < 1) {return nil}
if filtered.count < 1 { return nil }
return filtered[0]
}

Expand All @@ -310,7 +310,7 @@ struct Util {

//MARK: Years
static func getinactiveYears(_ sub: UserSubject)-> [String]{
if(sub.inactiveYears.isEmpty){return []}
if sub.inactiveYears.isEmpty { return [] }
let arr: [String] = sub.inactiveYears.components(separatedBy: " ")
return arr
}
Expand Down Expand Up @@ -345,8 +345,8 @@ struct Util {
var num = 1

for i in 1...4 {
let tests = filterTests(sub, checkinactive: false).filter{$0.year == i}
if(tests.count > 0){ num = i}
let tests = filterTests(sub, checkinactive: false).filter{ $0.year == i }
if tests.count > 0 { num = i }
}
return num
}
Expand All @@ -358,8 +358,8 @@ struct Util {
//MARK: Dates
/// Returns the last date when a grade was added
static func calcMaxDate() -> Date {
let allSubjects = self.getAllSubjects().filter{$0.subjecttests?.count != 0}
if(allSubjects.count == 0) {return Date()}
let allSubjects = self.getAllSubjects().filter{ $0.subjecttests?.count != 0 }
if allSubjects.count == 0 { return Date() }

let allDates = allSubjects.map{
($0.subjecttests?.allObjects as? [UserTest] ?? [])}
Expand All @@ -368,15 +368,15 @@ struct Util {
$0.date.timeIntervalSince1970
}.sorted(by: {$0 > $1})[0]
}
if(allDates.count == 0) {return Date(timeIntervalSince1970: 0.0)}
if allDates.count == 0 { return Date(timeIntervalSince1970: 0.0) }

return Date(timeIntervalSince1970: allDates.sorted(by: {$0 > $1})[0])
}

/// Returns the first date when a grade was added
static func calcMinDate() -> Date {
let allSubjects = self.getAllSubjects().filter{$0.subjecttests?.count != 0}
if(allSubjects.count == 0){return Date()}
let allSubjects = self.getAllSubjects().filter{ $0.subjecttests?.count != 0 }
if allSubjects.count == 0 { return Date() }

let allDates = allSubjects.map{
($0.subjecttests?.allObjects as? [UserTest] ?? [])}
Expand All @@ -385,14 +385,14 @@ struct Util {
$0.date.timeIntervalSince1970
}.sorted(by: {$0 < $1})[0]
}
if(allDates.count == 0) { return Date(timeIntervalSince1970: 0.0) }
if allDates.count == 0 { return Date(timeIntervalSince1970: 0.0) }

return Date(timeIntervalSince1970: allDates.sorted(by: {$0 < $1})[0])
}

//MARK: Tests
static func filterTests(_ sub: UserSubject, checkinactive: Bool = true)-> [UserTest]{
if(sub.subjecttests == nil){return []}
static func filterTests(_ sub: UserSubject, checkinactive: Bool = true) -> [UserTest]{
if sub.subjecttests == nil { return [] }
var tests = sub.subjecttests!.allObjects as! [UserTest]

for year in [1,2,3,4]{
Expand Down Expand Up @@ -435,7 +435,7 @@ struct Util {
}

static func deleteType(type: Int16) {
let t = getTypes().filter{$0.id == type}[0]
let t = getTypes().filter{ $0.id == type }[0]
t.gradetosettings!.removeFromGradetypes(t)
saveCoreData()
}
Expand All @@ -459,10 +459,6 @@ struct Util {
return types.sorted(by: { $0.weigth > $1.weigth})
}

static func highestType() -> Int16 {
return getTypes().sorted(by: {$0.id > $1.id})[0].id
}

static func getTypeGrades(_ type: Int16) -> [UserTest] {
var arr: [UserTest] = []
for sub in Util.getAllSubjects() {
Expand Down Expand Up @@ -490,11 +486,11 @@ struct Util {

static func checkIfNewVersion() -> Bool {
let oldVersion = UserDefaults.standard.string(forKey: UD_lastVersion) ?? "0.0.0"
if(oldVersion == "0.0.0") { return true }
if oldVersion == "0.0.0" { return true }
let partsOldV = oldVersion.split(separator: ".")
let partsNewV = appVersion.split(separator: ".")

if(partsOldV.isEmpty) { return true }
if partsOldV.isEmpty { return true }

if(partsOldV[0] < partsNewV[0]){
return true;
Expand Down
16 changes: 16 additions & 0 deletions CalqTests/CalqTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,20 @@ final class CalqTests: XCTestCase {
let lastYear = Util.lastActiveYear(getExampleSub())
XCTAssertEqual(lastYear, 4)
}

// MARK: GradeTypes
func testGetDefaultTypes(){
let types = Util.getTypes()
XCTAssertEqual(types.count, 2)
XCTAssertEqual(types.filter {$0.name == "Test" || $0.name == "Klausur"}.count, 2)
}

func testEditTypes(){
//add type
Util.addType(name: "someName", weigth: 0)
let type = Util.getTypes().filter {$0.name == "someName"}[0]
//remove random type
Util.deleteType(type: type.id)
XCTAssertTrue(Util.getTypes().filter {$0.name == "someName"}.isEmpty)
}
}