<commit_before>//
//  main.swift
//  ASwiftPromise
//
//  Created by @JonathanADunlap on 6/4/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

// Cold Futures store history and replays it to new listeners
import Foundation

func simpleFuture() {
    var f = Future<Int>(isCold: false);
    f.val = 3; // thrown away since it's cold
    f.forEach(){
        if($0==3) {println("fail simpleFuture");}
        else if($0==5) {println("success simpleFuture");}
    }
    f.val = 5;
}; simpleFuture();

func simpleHotDefferedTest() {
    var def1 = Deffered<Int,Int>(isCold: false);
    def1.done(3); // thrown away since it's cold
    def1.promise.success.forEach(){
        if($0==3) {println("fail simpleHotDefferedTest");}
        else if($0==5) {println("success simpleHotDefferedTest");}
    }
    def1.done(5);
}; simpleHotDefferedTest();

func simpleColdDefferedTest() {
    var def1 = Deffered<Int,Int>(isCold: true);
    def1.done(3);
    var step = 0;
    def1.promise.success.forEach(){
        if($0==3 && step==0) {step++}
        else if($0==5 && step==1) {println("success simpleColdDefferedTest");}
        else {println("failed simpleColdDefferedTest")}
    }
    def1.done(5);
}; simpleColdDefferedTest();

func filterMapHotTest() {
    var def1 = Deffered<Int,Int>(isCold: false); //isCold: true for history playback
    var p = def1.promise;
    
    //var step = 0;
    var m = p.success
        .filter() { $0 < 100 }
        .map() { "My number is " + String($0) }
    
    m.forEach(){
        if($0=="My number is 105") {println("fail filterMapHotDeffered");}
        else if($0=="My number is 5") {println("success filterMapHotDeffered");}
        else {println("failed filterMapHotDeffered")}
    }
    
    def1.done(105);
    def1.done(5);
}; filterMapHotTest();

func mergeTest() {
    var def1 = Deffered<Int,Int>(isCold: false); //isCold: true for history playback
    
    //Merging
    var def2 = Deffered<String,String>();
    def2.promise.success.merge(def1.promise.success).forEach() {
        switch $0 {
        case(let x, let y):
            if(x=="Hello" && y==5) { println("success mergeTest"); }
        default:
            println("fail mergeTest");
        }
    }
    def1.done(5);
    def2.done("Hello");
}; mergeTest();

func foldTest() {
    var def1 = Deffered<Int,Int>(isCold: false);
    var step = 0;
    var f = def1.promise.success.fold({$0 + $1}, total:1)
        .forEach() {
            if(step==0 && $0==1+2) { step++; }
            else if(step==1 && $0==1+2+3) { step++; }
            else if(step==2 && $0==1+2+3+4) { println("success foldTest") }
            else { println("fail foldTest") }
        }
    
    def1.done(2);
    def1.done(3);
    def1.done(4);
}; foldTest();

println("completed");<commit_msg>added unit tests<commit_after>//
//  main.swift
//  ASwiftPromise
//
//  Created by @JonathanADunlap on 6/4/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

// Cold Futures store history and replays it to new listeners
import Foundation

func simpleFuture() {
    var f = Future<Int>(isCold: false);
    f.val = 3; // thrown away since it's cold
    f.forEach(){
        if($0==3) {println("fail simpleFuture");}
        else if($0==5) {println("success simpleFuture");}
    }
    f.val = 5;
}; simpleFuture();

func simpleHotDefferedTest() {
    var def1 = Deffered<Int,Int>(isCold: false);
    def1.done(3); // thrown away since it's cold
    def1.promise.success.forEach(){
        if($0==3) {println("fail simpleHotDefferedTest");}
        else if($0==5) {println("success simpleHotDefferedTest");}
    }
    def1.done(5);
}; simpleHotDefferedTest();

func simpleColdDefferedTest() {
    var def1 = Deffered<Int,Int>(isCold: true);
    def1.done(2); // will be stored for future listeners because it's cold
    def1.done(3);
    var step = 0;
    def1.promise.success.forEach(){
        if($0==2 && step==0) {step++}
        else if($0==3 && step==1) {step++}
        else if($0==5 && step==2) {println("success simpleColdDefferedTest");}
        else {println("failed simpleColdDefferedTest")}
    }
    def1.done(5);
}; simpleColdDefferedTest();

func filterMapHotTest() {
    var def1 = Deffered<Int,Int>(isCold: false); //isCold: true for history playback
    var p = def1.promise;
    
    //var step = 0;
    var m = p.success
        .filter() { $0 < 100 }
        .map() { "My number is " + String($0) }
    
    m.forEach(){
        if($0=="My number is 105") {println("fail filterMapHotDeffered");}
        else if($0=="My number is 5") {println("success filterMapHotDeffered");}
        else {println("failed filterMapHotDeffered")}
    }
    
    def1.done(105);
    def1.done(5);
}; filterMapHotTest();

func mergeTest() {
    var def1 = Deffered<Int,Int>(isCold: false); //isCold: true for history playback
    
    //Merging
    var def2 = Deffered<String,String>();
    def2.promise.success.merge(def1.promise.success).forEach() {
        switch $0 {
        case(let x, let y):
            if(x=="Hello" && y==5) { println("success mergeTest"); }
        default:
            println("fail mergeTest");
        }
    }
    def1.done(5);
    def2.done("Hello");
}; mergeTest();

func foldTest() {
    var def1 = Deffered<Int,Int>(isCold: false);
    var step = 0;
    var f = def1.promise.success.fold({$0 + $1}, total:1)
        .forEach() {
            if(step==0 && $0==1+2) { step++; }
            else if(step==1 && $0==1+2+3) { step++; }
            else if(step==2 && $0==1+2+3+4) { println("success foldTest") }
            else { println("fail foldTest") }
        }
    
    def1.done(2);
    def1.done(3);
    def1.done(4);
}; foldTest();

println("completed");
{'fraction_non_alphanumeric': 0.1759014951627089, 'fraction_numerical': 0.033597185576077396, 'mean_word_length': 2.7018229166666665, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, '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': '4976486', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1935, 'n_words': 439}