<commit_before>﻿open NUnit.Framework
open Yard.Generators.CYKGenerator
open Yard.Frontends.YardFrontend
open System.IO

[<TestFixture>]
type CYKTests () =
    let generator = new Yard.Generators.CYKGenerator.CYKGeneartorImpl()
    let parser = new Yard.Frontends.YardFrontend.YardFrontend()
    let basePath = "../../../../Tests/CYK"

    [<Test>]
    member test.test1 () =
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_2.yrd"))
        let rules,start = generator.GenRulesList il
        let code = generator.Generate(il)
        let input = [|1us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        Assert.IsTrue(true)
        //let expected = "E->EE  conflict E->EE  l1 E->TX  l1 T->a   X->*   E->TX  l1 T->a   X->*   E->TY  l2 T->a   Y->+  "
        //let testGrammar_lbl1 = (Array.ofList <| (List.map ToBranch [("E","T","X",Some("l1"));("E","T","Y",Some("l2"));("E","E","E",None)]) @ (List.map ToLeaf [("T",'a',None);("X",'*',None);("Y",'+',None)]),"E")
        //let result = parser.Recognize testGrammar_lbl1 "a*a*a+" |> fun (s:string) -> s.Replace( "\n", " ")
        //Assert.AreEqual(expected, result)

    [<Test>]
    member test.noLbl () =
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|1us;1us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        let expected = "undefined 0 0" 
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.oneLbl() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_oneLbl.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|1us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        let expected = "defined 1 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.oneLblNoInputLbl() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_oneLbl.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|2us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        let expected = "undefined 0 0"
        Assert.AreEqual(expected,res)
        
    [<Test>]
    member test.twoLbls() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_twoLbls.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|1us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        let expected = "defined 1 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.twoLblsOneInputDial() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_twoLblsTwoDial.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|1us;1us;|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        let expected = "defined 1 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.twoLblsTwoDials() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_twoLblsTwoDial.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|1us;2us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        let expected = "conflict 0 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.labelTracking() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "labelTracking.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|1us;2us;2us;2us;1us;2us;2us;2us;1us;2us;2us;2us;|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        Assert.IsTrue true
        //System.Console.ReadKey()

    [<Test>]
    member test.labelTracking2() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "labelTracking2.yrd"))
        let rules,start = generator.GenRulesList il
        let input = [|1us;2us;2us;2us;|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy)
        Assert.IsTrue true
        //System.Console.ReadKey()

[<EntryPoint>]
let f _ =
    let tests = new CYKTests()
//    let x1 = tests.noLbl()
//    let x2 = tests.oneLbl()
//    let x3 = tests.oneLblNoInputLbl()
//    let x4 = tests.twoLbls()
//    let x5 = tests.twoLblsOneInputDial()
//    let x6 = tests.twoLblsTwoDials()
    let x7 = tests.labelTracking();
    //let x8 = tests.labelTracking2();
    0

<commit_msg>CYKCore tests fixed.<commit_after>﻿open NUnit.Framework
open Yard.Generators.CYKGenerator
open Yard.Frontends.YardFrontend
open System.IO

[<TestFixture>]
type CYKTests () =
    let generator = new Yard.Generators.CYKGenerator.CYKGeneartorImpl()
    let parser = new Yard.Frontends.YardFrontend.YardFrontend()
    let basePath = "../../../../Tests/CYK"

    [<Test>]
    member test.test1 () =
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_2.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let code = generator.Generate(il)
        let input = [|1us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        Assert.IsTrue(true)
        //let expected = "E->EE  conflict E->EE  l1 E->TX  l1 T->a   X->*   E->TX  l1 T->a   X->*   E->TY  l2 T->a   Y->+  "
        //let testGrammar_lbl1 = (Array.ofList <| (List.map ToBranch [("E","T","X",Some("l1"));("E","T","Y",Some("l2"));("E","E","E",None)]) @ (List.map ToLeaf [("T",'a',None);("X",'*',None);("Y",'+',None)]),"E")
        //let result = parser.Recognize testGrammar_lbl1 "a*a*a+" |> fun (s:string) -> s.Replace( "\n", " ")
        //Assert.AreEqual(expected, result)

    [<Test>]
    member test.noLbl () =
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|1us;1us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        let expected = "undefined 0 0" 
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.oneLbl() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_oneLbl.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|1us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        let expected = "defined 1 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.oneLblNoInputLbl() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_oneLbl.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|2us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        let expected = "undefined 0 0"
        Assert.AreEqual(expected,res)
        
    [<Test>]
    member test.twoLbls() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_twoLbls.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|1us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        let expected = "defined 1 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.twoLblsOneInputDial() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_twoLblsTwoDial.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|1us;1us;|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        let expected = "defined 1 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.twoLblsTwoDials() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "simple_test_twoLblsTwoDial.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|1us;2us;2us|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        let expected = "conflict 0 0"
        Assert.AreEqual(expected,res)

    [<Test>]
    member test.labelTracking() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "labelTracking.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|1us;2us;2us;2us;1us;2us;2us;2us;1us;2us;2us;2us;|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        Assert.IsTrue true
        //System.Console.ReadKey()

    [<Test>]
    member test.labelTracking2() = 
        let il = parser.ParseGrammar(Path.Combine(basePath, "labelTracking2.yrd"))
        let rules,start,lbls = generator.GenRulesList il
        let input = [|1us;2us;2us;2us;|]
        let res = (new CYKCore()).Recognize (rules,1us) input (fun l1 l2 l3 -> 0uy) lbls
        Assert.IsTrue true
        //System.Console.ReadKey()

[<EntryPoint>]
let f _ =
//    let tests = new CYKTests()
//    let x1 = tests.noLbl()
//    let x2 = tests.oneLbl()
//    let x3 = tests.oneLblNoInputLbl()
//    let x4 = tests.twoLbls()
//    let x5 = tests.twoLblsOneInputDial()
//    let x6 = tests.twoLblsTwoDials()
//    let x7 = tests.labelTracking();
    //let x8 = tests.labelTracking2();
    0


{'fraction_non_alphanumeric': 0.15214829101039323, 'fraction_numerical': 0.023572270438229938, 'mean_word_length': 2.7410821643286574, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 69, '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': '2347164', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3402, 'n_words': 737}