<commit_before>//
//  kNNRealmTests.swift
//  kNNRealm
//
//  Created by <NAME> on 6/26/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Quick
import Nimble
import kNNRealm
import RealmSwift
import Darwin

class GeneratedDataSetSpec: QuickSpec {

  override func spec() {
    let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let realmPath = documentsDirectory[0].stringByAppendingPathComponent("generated_data_set_spec.realm")

    let data: [Dog] = (1...9).map { i in
      let dog = Dog()
      dog.height = Double(i)
      dog.weight = Double(i)
      dog.bodyLength = Double(i)

      switch i {
      case 1...3:
        dog.name = "small"
        break

      case 4...6:
        dog.name = "medium"
        break

      default:
        dog.name = "large"
        break
      }

      return dog
    }

    let k = 3

    let query = { (q: Dog, results: Results<Dog>) -> Results<Dog> in
      return results
        .filter("height >= \(q.height - 2)")
        .filter("height <= \(q.height + 2)")
        .filter("weight >= \(q.weight - 2)")
        .filter("weight <= \(q.weight + 2)")
        .filter("bodyLength >= \(q.bodyLength - 2)")
        .filter("bodyLength <= \(q.bodyLength + 2)")
    }

    let distance = { (l: Dog, r: Dog) -> Double in
      return pow(l.height - r.height, 2) + pow(l.weight - r.weight, 2) + pow(l.bodyLength - r.bodyLength, 2)
    }

    var knn: kNNRealm<Dog>?

    beforeEach {
      // set test data to realm
      deleteRealmFilesAtPath(realmPath)
      knn = kNNRealm<Dog>(realm: Realm(path: realmPath), k: k, query: query, distance: distance)
    }

    afterEach {
      // clear all realm
      deleteRealmFilesAtPath(realmPath)
    }

    describe("basic") {
      describe("add data by kNNRealm") {
        it("can be added data") {
          knn?.add(data)
          expect(Realm(path: realmPath).objects(Dog).count) == data.count
        }

        it("can search correct label") {
          knn?.add(data)

          for dog in data {
            let nearestDogs = knn?.search(dog)
            expect(nearestDogs?.count) == k
          }

          for dog in data {
            let nearestDogs = knn?.search(dog)
            let actual = nearestDogs?.map { dog in Int(dog.height) }
            let i = Int(dog.height)
            switch i {
            case 1:
              expect(actual) == [1, 2, 3]
              break

            case 2...8:
              expect(actual) == [i, i-1, i+1]
              break

            case 9:
              expect(actual) == [9, 8, 7]
              break

            default:
              break
            }
          }
        }
      }

      describe("start from already built realm") {
        beforeEach {
          let realm = Realm(path: realmPath)
          realm.write {
            realm.add(data)
          }
        }

        it ("can search") {
          for dog in data {
            let nearestDogs = knn?.search(dog)
            expect(nearestDogs?.count) == k
          }

          for dog in data {
            let nearestDogs = knn?.search(dog)
            let actual = nearestDogs?.map { dog in Int(dog.height) }
            let i = Int(dog.height)
            switch i {
            case 1:
              expect(actual) == [1, 2, 3]
              break

            case 2...8:
              expect(actual) == [i, i-1, i+1]
              break
              
            case 9:
              expect(actual) == [9, 8, 7]
              break
              
            default:
              break
            }
          }
        }
      }
    }
  }
}<commit_msg>Refactored the test<commit_after>//
//  kNNRealmTests.swift
//  kNNRealm
//
//  Created by <NAME> on 6/26/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Quick
import Nimble
import kNNRealm
import RealmSwift
import Darwin

class GeneratedDataSetSpec: QuickSpec {

  override func spec() {
    let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let realmPath = documentsDirectory[0].stringByAppendingPathComponent("generated_data_set_spec.realm")

    let data: [Dog] = (1...9).map { i in
      let dog = Dog()
      dog.height = Double(i)
      dog.weight = Double(i)
      dog.bodyLength = Double(i)

      switch i {
      case 1...3:
        dog.name = "small"
        break

      case 4...6:
        dog.name = "medium"
        break

      default:
        dog.name = "large"
        break
      }

      return dog
    }

    let k = 3

    let query = { (q: Dog, results: Results<Dog>) -> Results<Dog> in
      return results
        .filter("height >= \(q.height - 2)")
        .filter("height <= \(q.height + 2)")
        .filter("weight >= \(q.weight - 2)")
        .filter("weight <= \(q.weight + 2)")
        .filter("bodyLength >= \(q.bodyLength - 2)")
        .filter("bodyLength <= \(q.bodyLength + 2)")
    }

    let distance = { (l: Dog, r: Dog) -> Double in
      return pow(l.height - r.height, 2) + pow(l.weight - r.weight, 2) + pow(l.bodyLength - r.bodyLength, 2)
    }

    var knn: kNNRealm<Dog>?

    let checkSearchCorrectly = { () -> Void in
      for dog in data {
        let nearestDogs = knn?.search(dog)
        expect(nearestDogs?.count) == k
      }

      for dog in data {
        let nearestDogs = knn?.search(dog)
        let actual = nearestDogs?.map { dog in Int(dog.height) }
        let i = Int(dog.height)
        switch i {
        case 1:
          expect(actual) == [1, 2, 3]
          break

        case 2...8:
          expect(actual) == [i, i-1, i+1]
          break

        case 9:
          expect(actual) == [9, 8, 7]
          break

        default:
          break
        }
      }
    }

    beforeEach {
      deleteRealmFilesAtPath(realmPath)
      knn = kNNRealm<Dog>(realm: Realm(path: realmPath), k: k, query: query, distance: distance)
    }

    afterEach {
      deleteRealmFilesAtPath(realmPath)
    }

    describe("basic") {
      describe("when added data by kNNRealm") {
        it("works") {
          knn?.add(data)
          expect(Realm(path: realmPath).objects(Dog).count) == data.count
        }

        it("can search correctly") {
          knn?.add(data)
          checkSearchCorrectly()
        }
      }

      describe("when with pre-built realm") {
        beforeEach {
          let realm = Realm(path: realmPath)
          realm.write {
            realm.add(data)
          }
        }
        
        it ("can search correctly") {
          checkSearchCorrectly()
        }
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.12754716981132075, 'fraction_numerical': 0.013283018867924528, 'mean_word_length': 1.5652342237708092, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3724659', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2045, 'n_words': 505}