Example #1
0
        public void Traits_must_belong_to_the_same_context()
        {
            Assert.Throws<ArgumentException>( () => new CKTraitContext( null ) );
            Assert.Throws<ArgumentException>( () => new CKTraitContext( "  " ) );

            CKTraitContext c1 = new CKTraitContext( "C1" );
            CKTraitContext c2 = new CKTraitContext( "C2" );

            var t1 = c1.FindOrCreate( "T1" );
            var t2 = c2.FindOrCreate( "T2" );
            Assert.That( t1 != t2 );
            Assert.Throws<InvalidOperationException>( () => t1.Union( t2 ) );
            Assert.Throws<InvalidOperationException>( () => t1.Intersect( t2 ) );
            Assert.Throws<InvalidOperationException>( () => t1.Except( t2 ) );
            Assert.Throws<InvalidOperationException>( () => t1.SymmetricExcept( t2 ) );

            Assert.Throws<InvalidOperationException>( () => t1.Overlaps( t2 ) );
            Assert.Throws<InvalidOperationException>( () => t1.IsSupersetOf( t2 ) );

            Assert.Throws<ArgumentNullException>( () => t1.Union( null ) );
            Assert.Throws<ArgumentNullException>( () => t1.Intersect( null ) );
            Assert.Throws<ArgumentNullException>( () => t1.Except( null ) );
            Assert.Throws<ArgumentNullException>( () => t1.SymmetricExcept( null ) );

            Assert.Throws<ArgumentNullException>( () => t1.Overlaps( null ) );
            Assert.Throws<ArgumentNullException>( () => t1.IsSupersetOf( null ) );

            Assert.Throws<ArgumentNullException>( () => t1.CompareTo( null ) );
            Assert.Throws<ArgumentNullException>( () => c1.CompareTo( null ) );
        }
 internal DBCKTraitContext(Package p, CKTraitContext context, int contextId)
 {
     Package          = p;
     Context          = context;
     CKTraitContextId = contextId;
     EmptyTrait       = new DBCKTrait(0, context.EmptyTrait);
 }
Example #3
0
        public void Traits_must_belong_to_the_same_context()
        {
            Assert.Throws <ArgumentException>(() => new CKTraitContext(null));
            Assert.Throws <ArgumentException>(() => new CKTraitContext("  "));

            CKTraitContext c1 = new CKTraitContext("C1");
            CKTraitContext c2 = new CKTraitContext("C2");

            var t1 = c1.FindOrCreate("T1");
            var t2 = c2.FindOrCreate("T2");

            Assert.That(t1 != t2);
            Assert.Throws <InvalidOperationException>(() => t1.Union(t2));
            Assert.Throws <InvalidOperationException>(() => t1.Intersect(t2));
            Assert.Throws <InvalidOperationException>(() => t1.Except(t2));
            Assert.Throws <InvalidOperationException>(() => t1.SymmetricExcept(t2));

            Assert.Throws <InvalidOperationException>(() => t1.Overlaps(t2));
            Assert.Throws <InvalidOperationException>(() => t1.IsSupersetOf(t2));

            Assert.Throws <ArgumentNullException>(() => t1.Union(null));
            Assert.Throws <ArgumentNullException>(() => t1.Intersect(null));
            Assert.Throws <ArgumentNullException>(() => t1.Except(null));
            Assert.Throws <ArgumentNullException>(() => t1.SymmetricExcept(null));

            Assert.Throws <ArgumentNullException>(() => t1.Overlaps(null));
            Assert.Throws <ArgumentNullException>(() => t1.IsSupersetOf(null));

            Assert.Throws <ArgumentNullException>(() => t1.CompareTo(null));
            Assert.Throws <ArgumentNullException>(() => c1.CompareTo(null));
        }
Example #4
0
        public void trait_separator_can_be_changed_from_the_default_pipe()
        {
            var     c = new CKTraitContext("SemiColonContext", ';');
            CKTrait m = c.FindOrCreate("Beta;Alpha;Fridge;Combo");

            c.EmptyTrait.IsSupersetOf(c.EmptyTrait).Should().BeTrue("Empty is contained by definition in itself.");
            m.IsSupersetOf(c.EmptyTrait).Should().BeTrue("Empty is contained by definition.");
            m.IsSupersetOf(c.FindOrCreate("Fridge;Alpha")).Should().BeTrue();
            m.IsSupersetOf(c.FindOrCreate("Fridge")).Should().BeTrue();
            m.IsSupersetOf(c.FindOrCreate("Fridge;Alpha;Combo")).Should().BeTrue();
            m.IsSupersetOf(c.FindOrCreate("Fridge;Alpha;Beta;Combo")).Should().BeTrue();
            m.IsSupersetOf(c.FindOrCreate("Fridge;Lol")).Should().BeFalse();
            m.IsSupersetOf(c.FindOrCreate("Murfn")).Should().BeFalse();
            m.IsSupersetOf(c.FindOrCreate("Fridge;Alpha;Combo+Lol")).Should().BeFalse();
            m.IsSupersetOf(c.FindOrCreate("Lol;Fridge;Alpha;Beta;Combo")).Should().BeFalse();

            m.Overlaps(c.FindOrCreate("Fridge;Alpha")).Should().BeTrue();
            m.Overlaps(c.FindOrCreate("Nimp;Fridge;Mourfn")).Should().BeTrue();
            m.Overlaps(c.FindOrCreate("Fridge;Alpha;Combo;Albert")).Should().BeTrue();
            m.Overlaps(c.FindOrCreate("ZZF;AAlp;BBeBe;Combo")).Should().BeTrue();
            m.Overlaps(c.FindOrCreate("AFridge;ALol")).Should().BeFalse();
            m.Overlaps(c.FindOrCreate("Murfn")).Should().BeFalse();
            m.Overlaps(c.FindOrCreate("QF;QA;QC;QL")).Should().BeFalse();
            m.Overlaps(c.EmptyTrait).Should().BeFalse("Empty is NOT contained 'ONE' since EmptyTrait.AtomicTraits.Count == 0...");
            c.EmptyTrait.Overlaps(c.EmptyTrait).Should().BeFalse("Empty is NOT contained 'ONE' in itself.");
        }
Example #5
0
        public void Traits_must_belong_to_the_same_context()
        {
            Action a = () => new CKTraitContext(null);

            a.Should().Throw <ArgumentException>();

            a = () => new CKTraitContext("  ");
            a.Should().Throw <ArgumentException>();

            CKTraitContext c1 = new CKTraitContext("C1");
            CKTraitContext c2 = new CKTraitContext("C2");

            var t1 = c1.FindOrCreate("T1");
            var t2 = c2.FindOrCreate("T2");

            t1.Should().NotBeSameAs(t2);
            t1.Invoking(sut => sut.Union(t2)).Should().Throw <InvalidOperationException>();
            t1.Invoking(sut => sut.Intersect(t2)).Should().Throw <InvalidOperationException>();
            t1.Invoking(sut => sut.Except(t2)).Should().Throw <InvalidOperationException>();
            t1.Invoking(sut => sut.SymmetricExcept(t2)).Should().Throw <InvalidOperationException>();

            t1.Invoking(sut => sut.Overlaps(t2)).Should().Throw <InvalidOperationException>();
            t1.Invoking(sut => sut.IsSupersetOf(t2)).Should().Throw <InvalidOperationException>();

            t1.Invoking(sut => sut.Union(null)).Should().Throw <ArgumentNullException>();
            t1.Invoking(sut => sut.Intersect(null)).Should().Throw <ArgumentNullException>();
            t1.Invoking(sut => sut.Except(null)).Should().Throw <ArgumentNullException>();
            t1.Invoking(sut => sut.SymmetricExcept(null)).Should().Throw <ArgumentNullException>();

            t1.Invoking(sut => sut.Overlaps(null)).Should().Throw <ArgumentNullException>();
            t1.Invoking(sut => sut.IsSupersetOf(null)).Should().Throw <ArgumentNullException>();

            t1.Invoking(sut => sut.CompareTo(null)).Should().Throw <ArgumentNullException>();
            t1.Invoking(sut => sut.CompareTo(null)).Should().Throw <ArgumentNullException>();
        }
Example #6
0
        public void binary_operators_work()
        {
            var ctx = CKTraitContext.Create( "Indep", ',', shared: false );
            var a = ctx.FindOrCreate( "A" );
            var b = ctx.FindOrCreate( "B" );
            var c = ctx.FindOrCreate( "C" );
            var d = ctx.FindOrCreate( "D" );
            var ab = ctx.FindOrCreate( "A,B" );
            var cd = ctx.FindOrCreate( "C,D" );
            var abc = ctx.FindOrCreate( "A,B,C" );
            var abcd = ctx.FindOrCreate( "A,B,C,D" );
            (ab + cd).Should()
                    .BeSameAs( abcd )
                    .And.BeSameAs( c + d + b + a )
                    .And.BeSameAs( cd + ab )
                    .And.BeSameAs( abc | d )
                    .And.BeSameAs( a | b | c | d );

            (ab & cd).Should().BeSameAs( ctx.EmptyTrait );
            (abc & cd).Should().BeSameAs( c );
            (abcd & cd).Should().BeSameAs( cd );

            (ab ^ cd).Should().BeSameAs( abcd );
            (abc ^ cd).Should().BeSameAs( a | b | d );
            ((a|b|d) ^ cd).Should().BeSameAs( abc );
            (abcd ^ cd).Should().BeSameAs( ab );

            CKTrait v = a;
            v += b;
            v.Should().BeSameAs( ab );
            v += d;
            v.Should().BeSameAs( a | b | d );
            v -= c;
            v.Should().BeSameAs( a | b | d );
            v -= abcd;
            v.IsEmpty.Should().BeTrue();

            v |= abcd;
            v &= cd;
            v.Should().BeSameAs( cd );
            v &= d;
            v.Should().BeSameAs( d );
            v |= abcd;
            v.Should().BeSameAs( abcd );
            v ^= b;
            v.Should().BeSameAs( a | cd );
            v ^= ab;
            v.Should().BeSameAs( b | cd );

            v &= ctx.EmptyTrait;
            v.IsEmpty.Should().BeTrue();
        }
        /// <summary>
        /// Registers a <see cref="CKTraitContext"/>. Finds or it creates it and handle separator
        /// change as needed.
        /// </summary>
        /// <param name="c">The call context to use.</param>
        /// <param name="actorId">The current actor identifier.</param>
        /// <param name="traitContext">The context to register.</param>
        /// <returns>The mapped context.</returns>
        public DBCKTraitContext RegisterContext(ISqlCallContext c, int actorId, CKTraitContext traitContext)
        {
            if (traitContext == null)
            {
                throw new ArgumentNullException(nameof(traitContext));
            }
            var db = _cache.GetOrAdd(traitContext.Name, name =>
            {
                return(new DBCKTraitContext(_package, traitContext, RegisterContext(c, actorId, name, traitContext.Separator)));
            });

            return(db);
        }
Example #8
0
        public void FindIfAllExist_tests()
        {
            var c = CKTraitContext.Create( "Indep", '+', shared: false );

            CKTrait m = c.FindOrCreate( "Alpha+Beta+Combo+Fridge" );

            c.FindIfAllExist( "" ).Should().Be( c.EmptyTrait );
            c.FindIfAllExist( "bo" ).Should().BeNull();
            var alpha = c.FindOrCreate( "Alpha" );
            c.FindIfAllExist( "Alpha" ).Should().Be( alpha );
            c.FindIfAllExist( "bo+pha" ).Should().BeNull();
            c.FindIfAllExist( "Fridge+Combo+Alpha+Beta" ).Should().BeSameAs( m );
        }
Example #9
0
 public void comparison_operators_work_cross_contexts()
 {
     var c = CKTraitContext.Create( "Indep", '+', shared: false );
     var cAfter = CKTraitContext.Create( "Indep+", '+', shared: false );
     var t1 = cAfter.FindOrCreate( "B" );
     var t2 = c.FindOrCreate( "B" );
     //
     (t1 > t2).Should().BeTrue( "Tag with same name relies on context's separator and then name ('greater' separator and then name are better)." );
     (t1 >= t2).Should().BeTrue();
     (t1 != t2).Should().BeTrue();
     (t1 == t2).Should().BeFalse();
     (t1 < t2).Should().BeFalse();
     (t1 <= t2).Should().BeFalse();
 }
Example #10
0
        public CKTrait ReadCKTrait()
        {
            var stateT = TraitPool.TryRead(out var t);

            if (!stateT.Success)
            {
                var stateC = TraitContextPool.TryRead(out var ctx);
                if (!stateC.Success)
                {
                    stateC.SetReadResult(ctx = CKTraitContext.Read(Reader));
                }
                stateT.SetReadResult(t = ctx.FindOrCreate(Reader.ReadSharedString()));
            }
            return(t);
        }
Example #11
0
        public void independent_or_shared_contexts()
        {
            var shared = CKTraitContext.Create( "Shared", '+' );
            Action notPossible = () => CKTraitContext.Create( "Shared", '-' );
            notPossible.Should().Throw<InvalidOperationException>();

            var here = shared.FindOrCreate( "Here!" );

            var independent1 = CKTraitContext.Create( "Shared", '-', shared: false );
            independent1.FindOnlyExisting( "Here!" ).Should().BeNull();

            independent1.FindOrCreate( "In Independent n°1" );

            var independent2 = CKTraitContext.Create( "Shared", '-', shared: false );
            independent2.FindOnlyExisting( "Here!" ).Should().BeNull();
            independent2.FindOnlyExisting( "In Independent n°1" ).Should().BeNull();

            var anotherShared = CKTraitContext.Create( "Shared", '+' );
            anotherShared.FindOnlyExisting( "Here!" ).Should().BeSameAs( here );

            (anotherShared == shared).Should().BeTrue();
            (anotherShared != shared).Should().BeFalse();
            anotherShared.Equals( shared ).Should().BeTrue();
            anotherShared.CompareTo( shared ).Should().Be( 0 );

            var here1 = independent1.FindOrCreate( "Here!" );
            var here2 = independent2.FindOrCreate( "Here!" );

            here.Should().NotBeSameAs( here1 );
            here.Should().NotBeSameAs( here2 );
            here1.Should().NotBeSameAs( here2 );

            // Comparisons across contexts: a unique index "sorts" the independent contexts.
            // This index is positive for independent contexts: shared context's tags come first.

            here.CompareTo( here1 ).Should().BeNegative();
            here1.CompareTo( here2 ).Should().BeNegative();
            here.CompareTo( here2 ).Should().BeNegative();

            here1.CompareTo( here ).Should().BePositive();
            here2.CompareTo( here1 ).Should().BePositive();
            here2.CompareTo( here ).Should().BePositive();
        }
Example #12
0
        public void Comparing_tags()
        {
            CKTraitContext c1 = CKTraitContext.Create( "C1" );
            CKTraitContext c2 = CKTraitContext.Create( "C2" );

            c1.CompareTo( c1 ).Should().Be( 0 );
            c1.CompareTo( c2 ).Should().BeLessThan( 0 );

            var tAc1 = c1.FindOrCreate( "A" );
            var tBc1 = c1.FindOrCreate( "B" );
            var tABc1 = c1.FindOrCreate( "A|B" );
            var tAc2 = c2.FindOrCreate( "A" );

            tAc1.CompareTo( tAc1 ).Should().Be( 0 );
            tAc1.CompareTo( tBc1 ).Should().BeGreaterThan( 0, "In the same context, A is stronger than B." );
            tABc1.CompareTo( tBc1 ).Should().BeGreaterThan( 0, "In the same context, A|B is stronger than B." );
            tAc1.CompareTo( tAc2 ).Should().BeLessThan( 0, "Between different contexts, the context ordering drives the ordering." );
            tABc1.CompareTo( tAc2 ).Should().BeLessThan( 0, "Between different contexts, the context ordering drives the ordering." );
        }
Example #13
0
        public void Comparing_traits()
        {
            CKTraitContext c1 = new CKTraitContext( "C1" );
            CKTraitContext c1Bis = new CKTraitContext( "C1" );
            CKTraitContext c2 = new CKTraitContext( "C2" );

            Assert.That( c1.CompareTo( c1 ), Is.EqualTo( 0 ) );
            Assert.That( c1.CompareTo( c2 ), Is.LessThan( 0 ) );
            Assert.That( c1Bis.CompareTo( c1 ), Is.GreaterThan( 0 ) );

            var tAc1 = c1.FindOrCreate( "A" );
            var tBc1 = c1.FindOrCreate( "B" );
            var tABc1 = c1.FindOrCreate( "A|B" );
            var tAc2 = c2.FindOrCreate( "A" );

            Assert.That( tAc1.CompareTo( tAc1 ), Is.EqualTo( 0 ) );
            Assert.That( tAc1.CompareTo( tBc1 ), Is.GreaterThan( 0 ), "In the same context, A is stronger than B." );
            Assert.That( tABc1.CompareTo( tBc1 ), Is.GreaterThan( 0 ), "In the same context, A|B is stronger than B." );
            Assert.That( tAc1.CompareTo( tAc2 ), Is.LessThan( 0 ), "Between different contexts, the context ordering drives the ordering." );
            Assert.That( tABc1.CompareTo( tAc2 ), Is.LessThan( 0 ), "Between different contexts, the context ordering drives the ordering." );
        }
Example #14
0
        public void Comparing_traits()
        {
            CKTraitContext c1    = new CKTraitContext("C1");
            CKTraitContext c1Bis = new CKTraitContext("C1");
            CKTraitContext c2    = new CKTraitContext("C2");

            Assert.That(c1.CompareTo(c1), Is.EqualTo(0));
            Assert.That(c1.CompareTo(c2), Is.LessThan(0));
            Assert.That(c1Bis.CompareTo(c1), Is.GreaterThan(0));

            var tAc1  = c1.FindOrCreate("A");
            var tBc1  = c1.FindOrCreate("B");
            var tABc1 = c1.FindOrCreate("A|B");
            var tAc2  = c2.FindOrCreate("A");

            Assert.That(tAc1.CompareTo(tAc1), Is.EqualTo(0));
            Assert.That(tAc1.CompareTo(tBc1), Is.GreaterThan(0), "In the same context, A is stronger than B.");
            Assert.That(tABc1.CompareTo(tBc1), Is.GreaterThan(0), "In the same context, A|B is stronger than B.");
            Assert.That(tAc1.CompareTo(tAc2), Is.LessThan(0), "Between different contexts, the context ordering drives the ordering.");
            Assert.That(tABc1.CompareTo(tAc2), Is.LessThan(0), "Between different contexts, the context ordering drives the ordering.");
        }
Example #15
0
 public void comparison_operators_work()
 {
     var c = CKTraitContext.Create( "Indep", '+', shared: false );
     var t1 = c.FindOrCreate( "A" );
     var t2 = c.FindOrCreate( "B" );
     //
     (t1 > t2).Should().BeTrue( "A is better than B." );
     (t1 >= t2).Should().BeTrue();
     (t1 != t2).Should().BeTrue();
     (t1 == t2).Should().BeFalse();
     (t1 < t2).Should().BeFalse();
     (t1 <= t2).Should().BeFalse();
     //
     t2 = null;
     (t1 > t2).Should().BeTrue( "Any tag is better than null." );
     (t1 >= t2).Should().BeTrue();
     (t1 != t2).Should().BeTrue();
     (t1 == t2).Should().BeFalse();
     (t1 < t2).Should().BeFalse();
     (t1 <= t2).Should().BeFalse();
     //
     t1 = null;
     (t1 > t2).Should().BeFalse( "null is the same as null." );
     (t1 < t2).Should().BeFalse();
     (t1 >= t2).Should().BeTrue( "null is the same as null." );
     (t1 <= t2).Should().BeTrue();
     (t1 == t2).Should().BeTrue();
     (t1 != t2).Should().BeFalse();
     //
     t2 = c.FindOrCreate( "B" );
     (t1 > t2).Should().BeFalse( "null is smaller that any tag." );
     (t1 >= t2).Should().BeFalse();
     (t1 != t2).Should().BeTrue();
     (t1 == t2).Should().BeFalse();
     (t1 < t2).Should().BeTrue();
     (t1 <= t2).Should().BeTrue();
 }
Example #16
0
        public void trait_separator_can_be_changed_from_the_default_pipe()
        {
            var c = new CKTraitContext( "SemiColonContext", ';' );
            CKTrait m = c.FindOrCreate( "Beta;Alpha;Fridge;Combo" );

            Assert.That( c.EmptyTrait.IsSupersetOf( c.EmptyTrait ), "Empty is contained by definition in itself." );
            Assert.That( m.IsSupersetOf( c.EmptyTrait ), "Empty is contained by definition." );
            Assert.That( m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha" ) ) );
            Assert.That( m.IsSupersetOf( c.FindOrCreate( "Fridge" ) ) );
            Assert.That( m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha;Combo" ) ) );
            Assert.That( m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha;Beta;Combo" ) ) );
            Assert.That( !m.IsSupersetOf( c.FindOrCreate( "Fridge;Lol" ) ) );
            Assert.That( !m.IsSupersetOf( c.FindOrCreate( "Murfn" ) ) );
            Assert.That( !m.IsSupersetOf( c.FindOrCreate( "Fridge;Alpha;Combo+Lol" ) ) );
            Assert.That( !m.IsSupersetOf( c.FindOrCreate( "Lol;Fridge;Alpha;Beta;Combo" ) ) );

            Assert.That( m.Overlaps( c.FindOrCreate( "Fridge;Alpha" ) ) );
            Assert.That( m.Overlaps( c.FindOrCreate( "Nimp;Fridge;Mourfn" ) ) );
            Assert.That( m.Overlaps( c.FindOrCreate( "Fridge;Alpha;Combo;Albert" ) ) );
            Assert.That( m.Overlaps( c.FindOrCreate( "ZZF;AAlp;BBeBe;Combo" ) ) );
            Assert.That( !m.Overlaps( c.FindOrCreate( "AFridge;ALol" ) ) );
            Assert.That( !m.Overlaps( c.FindOrCreate( "Murfn" ) ) );
            Assert.That( !m.Overlaps( c.FindOrCreate( "QF;QA;QC;QL" ) ) );
            Assert.That( !m.Overlaps( c.EmptyTrait ), "Empty is NOT contained 'ONE' since EmptyTrait.AtomicTraits.Count == 0..." );
            Assert.That( !c.EmptyTrait.Overlaps( c.EmptyTrait ), "Empty is NOT contained 'ONE' in itself." );
        }
Example #17
0
 public TraitsTests()
 {
     Context = new CKTraitContext( "Test", '+' );
 }
Example #18
0
 public TraitsTests()
 {
     Context = new CKTraitContext("Test", '+');
 }
Example #19
0
 public void Setup()
 {
     Context = new CKTraitContext("Test", '+');
 }
        /// <summary>
        /// Registers a <see cref="CKTraitContext"/>. Finds or it creates it and handle separator
        /// change as needed.
        /// </summary>
        /// <param name="c">The call context to use.</param>
        /// <param name="actorId">The current actor identifier.</param>
        /// <param name="traitContext">The context to register.</param>
        /// <returns>The mapped context.</returns>
        public async Task <DBCKTraitContext> RegisterContextAsync(ISqlCallContext c, int actorId, CKTraitContext traitContext)
        {
            if (!_cache.TryGetValue(traitContext.Name, out var db))
            {
                int id = await RegisterContextAsync(c, actorId, traitContext.Name, traitContext.Separator);

                db = new DBCKTraitContext(_package, traitContext, id);
                _cache.TryAdd(traitContext.Name, db);
            }
            return(db);
        }
Example #21
0
 public void Setup()
 {
     Context = new CKTraitContext( "Test", '+' );
 }
Example #22
0
 CKTraitContext ContextWithPlusSeparator() => CKTraitContext.Create( "Test", '+' );
Example #23
0
 public CKTrait ReadExistingTrait(CKTraitContext ctx) => ctx.FindOrCreate(Reader.ReadSharedString());