<commit_before>//
//  SelectTests.swift
//  Channels
//
//  Created by <NAME> on 2015-01-15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Darwin
import Foundation
import XCTest

@testable import Channels

class SelectExamples: XCTestCase
{
  func testSelect()
  {
    let a: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(0)
    let b: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(1)
    let c: (tx: Sender<Int>, rx: Receiver<Int>) = Channel.Wrap(SBufferedChan<Int>.Make(1))
    let d: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(2)
    let e: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(5)

    let channels = [a,b,c,d,e]

    let iterations = 10_000
    async {
      let senders = channels.map { $0.tx }
      for _ in 0..<iterations
      {
        let index = Int(arc4random_uniform(UInt32(senders.count)))
        senders[index] <- index
      }
      for sender in senders { sender.close() }
    }

    var messages = Array(count: channels.count, repeatedValue: 0)
    let selectables = channels.map { $0.rx as Selectable }
    while let selection = select(selectables)
    {
      switch selection.id
      {
      case let s where s === a.rx:
        if let p = a.rx.extract(selection) { messages[p] += 1 }

      case let s where s === b.rx:
        if let p = b.rx.extract(selection) { messages[p] += 1 }

      case let s where s === c.rx:
        if let p = c.rx.extract(selection) { messages[p] += 1 }

      case let s where s === d.rx:
        if let p = d.rx.extract(selection) { messages[p] += 1 }

      case let s where s === e.rx:
        if let p = e.rx.extract(selection) { messages[p] += 1 }

      default:
        continue // missed selection
      }
    }

    let i = messages.reduce(0, combine: +)
    syncprint("\(messages), \(i) total messages.")
    syncprintwait()
    
    XCTAssert(i == iterations, "Received \(i) messages; expected \(iterations)")
  }

  func testRandomBits()
  {
    let c0: (tx: Sender<Bool>, rx: Receiver<Bool>) = Channel<Bool>.Make(8)
    let c1: (tx: Sender<Bool>, rx: Receiver<Bool>) = Channel<Bool>.Make(8)

    dispatch_async(dispatch_get_global_queue(qos_class_self(), 0)) {
      for _ in 0..<8
      {
        if let selection = select(c0.tx, c1.tx)
        {
          switch selection.id
          {
          case let s where s === c0.tx:
            c0.tx.insert(selection, newElement: false)

          case let s where s === c1.tx:
            c1.tx.insert(selection, newElement: true)

          default: continue
          }
        }
      }
      c0.tx.close()
      c1.tx.close()
    }

    let merged = merge(c0.rx, c1.rx)
    while let b = <-merged
    {
      print("\(b ? 0:1)", appendNewline: false)
    }
    print("")
  }

  func testSendsAndReceives()
  {
    let capacity = 5
    let c: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(capacity)

    var cap = 0
    var count = 0

    while let selection = select([c.tx, c.rx], preventBlocking: false)
    {
      switch selection.id
      {
      case let s where s === c.tx:
        if c.tx.insert(selection, newElement: count)
        {
          print(++cap, appendNewline: false)
          if ++count > 30 { c.tx.close() }
        }
        else
        {
          XCTFail("Attempted to insert into a full channel (probably)")
        }

      case let s where s === c.rx:
        if let _ = c.rx.extract(selection)
        {
          print(--cap, appendNewline: false)
        }
        else
        {
          XCTFail("Attempted to extract from an empty channel (probably)")
        }

      default: continue
      }

      XCTAssert(cap >= 0 && cap <= capacity)
    }
    print("")
  }

  func testNonBlockingSends()
  {
    let c1 = Channel<UInt32>.Make()
    let c2 = Channel<UInt32>.Make()

    var attempts = 0
    dispatch_async(dispatch_get_global_queue(qos_class_self(), 0)) {
      while let selection = select([c1.tx,c2.tx], preventBlocking: true) where attempts < 1000
      {
        switch selection.id
        {
        case let s as Sender<UInt32> where s === c1.tx:
          s.insert(selection, newElement: arc4random())
        case let s as Sender<UInt32> where s === c2.tx:
          s.insert(selection, newElement: arc4random())
        default: usleep(1)
        }
        attempts += 1
      }
      c1.tx.close()
      c2.tx.close()
    }

    let merged = mergeRR(c1.rx, c2.rx)

    var messages = 0
    while let _ = <-merged
    {
      messages += 1
      usleep(100)
    }

    print("Sent \(messages) messages in \(attempts) attempts")
  }
}<commit_msg>Tweak the non-blocking example<commit_after>//
//  SelectTests.swift
//  Channels
//
//  Created by <NAME> on 2015-01-15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Darwin
import Foundation
import XCTest

@testable import Channels

class SelectExamples: XCTestCase
{
  func testSelect()
  {
    let a: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(0)
    let b: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(1)
    let c: (tx: Sender<Int>, rx: Receiver<Int>) = Channel.Wrap(SBufferedChan<Int>.Make(1))
    let d: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(2)
    let e: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(5)

    let channels = [a,b,c,d,e]

    let iterations = 10_000
    async {
      let senders = channels.map { $0.tx }
      for _ in 0..<iterations
      {
        let index = Int(arc4random_uniform(UInt32(senders.count)))
        senders[index] <- index
      }
      for sender in senders { sender.close() }
    }

    var messages = Array(count: channels.count, repeatedValue: 0)
    let selectables = channels.map { $0.rx as Selectable }
    while let selection = select(selectables)
    {
      switch selection.id
      {
      case let s where s === a.rx:
        if let p = a.rx.extract(selection) { messages[p] += 1 }

      case let s where s === b.rx:
        if let p = b.rx.extract(selection) { messages[p] += 1 }

      case let s where s === c.rx:
        if let p = c.rx.extract(selection) { messages[p] += 1 }

      case let s where s === d.rx:
        if let p = d.rx.extract(selection) { messages[p] += 1 }

      case let s where s === e.rx:
        if let p = e.rx.extract(selection) { messages[p] += 1 }

      default:
        continue // missed selection
      }
    }

    let i = messages.reduce(0, combine: +)
    syncprint("\(messages), \(i) total messages.")
    syncprintwait()
    
    XCTAssert(i == iterations, "Received \(i) messages; expected \(iterations)")
  }

  func testRandomBits()
  {
    let c0: (tx: Sender<Bool>, rx: Receiver<Bool>) = Channel<Bool>.Make(8)
    let c1: (tx: Sender<Bool>, rx: Receiver<Bool>) = Channel<Bool>.Make(8)

    dispatch_async(dispatch_get_global_queue(qos_class_self(), 0)) {
      for _ in 0..<8
      {
        if let selection = select(c0.tx, c1.tx)
        {
          switch selection.id
          {
          case let s where s === c0.tx:
            c0.tx.insert(selection, newElement: false)

          case let s where s === c1.tx:
            c1.tx.insert(selection, newElement: true)

          default: continue
          }
        }
      }
      c0.tx.close()
      c1.tx.close()
    }

    let merged = merge(c0.rx, c1.rx)
    while let b = <-merged
    {
      print("\(b ? 0:1)", appendNewline: false)
    }
    print("")
  }

  func testSendsAndReceives()
  {
    let capacity = 5
    let c: (tx: Sender<Int>, rx: Receiver<Int>) = Channel<Int>.Make(capacity)

    var cap = 0
    var count = 0

    while let selection = select([c.tx, c.rx], preventBlocking: false)
    {
      switch selection.id
      {
      case let s where s === c.tx:
        if c.tx.insert(selection, newElement: count)
        {
          print(++cap, appendNewline: false)
          if ++count > 30 { c.tx.close() }
        }
        else
        {
          XCTFail("Attempted to insert into a full channel (probably)")
        }

      case let s where s === c.rx:
        if let _ = c.rx.extract(selection)
        {
          print(--cap, appendNewline: false)
        }
        else
        {
          XCTFail("Attempted to extract from an empty channel (probably)")
        }

      default: continue
      }

      XCTAssert(cap >= 0 && cap <= capacity)
    }
    print("")
  }

  func testNonBlockingSends()
  {
    let c1 = Channel<UInt32>.Make()
    let c2 = Channel<UInt32>.Make()

    var attempts = 0
    dispatch_async(dispatch_get_global_queue(qos_class_self(), 0)) {
      while let selection = select([c1.tx,c2.tx], preventBlocking: true) where attempts < 1000
      {
        switch selection.id
        {
        case let s as Sender<UInt32> where s === c1.tx:
          s.insert(selection, newElement: arc4random())
        case let s as Sender<UInt32> where s === c2.tx:
          s.insert(selection, newElement: arc4random())
        default: break
        }
        attempts += 1
        usleep(1)
      }
      c1.tx.close()
      c2.tx.close()
    }

    let merged = merge(c1.rx, c2.rx)

    var messages = 0
    while let _ = <-merged
    {
      messages += 1
      usleep(100)
    }

    print("Sent \(messages) messages in \(attempts) attempts")
  }
}
{'fraction_non_alphanumeric': 0.13806172171088252, 'fraction_numerical': 0.02014076881429345, 'mean_word_length': 2.072521623419827, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 67, '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': '3498351', 'n_tokens_mistral': 3234, 'n_tokens_neox': 3061, 'n_words': 806}