axeefectushka

Untitled

Feb 25th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.22 KB | None | 0 0
  1. using System;
  2. public sealed class Adam : Male
  3. {
  4.     private static Adam instance;
  5.  
  6.     private Adam() : base()
  7.     {
  8.         Name = "Adam";
  9.     }
  10.     public static Adam GetInstance()
  11.     {
  12.         if (instance == null)
  13.         {
  14.             instance = new Adam();
  15.         }
  16.         return instance;
  17.     }
  18. }
  19. public sealed class Eve : Female
  20. {
  21.     private static Eve instance;
  22.    
  23.     private Eve() : base()
  24.     {
  25.         Name = "Eve";
  26.     }
  27.     public static Eve GetInstance(Adam adamInstance)
  28.     {
  29.         if (adamInstance == null) throw new ArgumentNullException();
  30.  
  31.         if (instance == null)
  32.         {
  33.             instance = new Eve();
  34.         }
  35.         return instance;
  36.     }
  37. }
  38. public class Male : Human
  39. {
  40.     public Male() {}
  41.     public Male(string name, Human mother, Human father)
  42.     {
  43.         if (mother is null) throw new ArgumentNullException();
  44.         if (father is null) throw new ArgumentNullException();
  45.         else
  46.         {
  47.             Name = name;
  48.             Mother = mother;
  49.             Father = father;
  50.         }
  51.     }
  52.  
  53. }
  54. public class Female : Human
  55. {
  56.     public Female() {}
  57.     public Female(string name, Female mother, Male father)
  58.     {
  59.         if(name=="Eve") throw new ArgumentNullException();
  60.         if (mother is null) throw new ArgumentNullException();
  61.         if (father is null) throw new ArgumentNullException();
  62.         else
  63.         {
  64.             Name = name;
  65.             Mother = mother;
  66.             Father = father;
  67.         }
  68.     }
  69. }
  70. public abstract class Human
  71. {
  72.     public string Name;
  73.     public Human Mother { get; set; }
  74.     public Human Father { get; set; }
  75. }
  76. /*
  77. using NUnit.Framework;
  78. using System;
  79. using System.Linq;
  80. using System.Reflection;
  81.  
  82. [TestFixture]
  83. public class SingletonTests
  84. {
  85.     [Test]
  86.     public void Adam_is_unique()
  87.     {
  88.         Adam adam = Adam.GetInstance();  
  89.         Adam anotherAdam = Adam.GetInstance();
  90.        
  91.         Assert.IsTrue(adam is Adam);
  92.         Assert.AreEqual(adam, anotherAdam);
  93.     }
  94.    
  95.     // Implement all the tests below one by one!
  96.    
  97.     /*
  98.     [Test]
  99.     public void Adam_is_unique_and_only_GetInstance_can_return_adam()
  100.     {  
  101.         // GetInstance() is the only static method on Adam
  102.         Assert.AreEqual(1, typeof(Adam).GetMethods().Where(x => x.IsStatic).Count());
  103.        
  104.         // Adam does not have public or internal constructors
  105.         Assert.IsFalse(typeof(Adam).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  106.           .Any(x => x.IsPublic || x.IsAssembly ));
  107.     }
  108.    
  109.     [Test]
  110.     public void Adam_is_unique_and_cannot_be_overriden()
  111.     {
  112.         Assert.IsTrue(typeof(Adam).IsSealed);
  113.     }
  114.    
  115.     [Test]
  116.     public void Adam_is_a_human()
  117.     {
  118.         Assert.IsTrue(Adam.GetInstance() is Human);
  119.     }
  120.  
  121.     [Test]
  122.     public void Adam_is_a_male()
  123.     {
  124.         Assert.IsTrue(Adam.GetInstance() is Male);
  125.     }
  126.  
  127.     [Test]
  128.     public void Eve_is_unique_and_created_from_the_rib_of_adam()
  129.     {
  130.         Adam adam = Adam.GetInstance();
  131.         Eve eve = Eve.GetInstance(adam);
  132.         Eve anotherEve = Eve.GetInstance(adam);
  133.        
  134.         Assert.IsTrue(eve is Eve);
  135.         Assert.AreEqual(eve, anotherEve);
  136.  
  137.         // GetInstance() is the only static method on Eve
  138.         Assert.AreEqual(1, typeof(Eve).GetMethods().Where(x => x.IsStatic).Count());
  139.  
  140.         // Eve has no public or internal constructor
  141.         Assert.IsFalse(typeof(Eve).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  142.           .Any(x => x.IsPublic || x.IsAssembly));
  143.        
  144.         // Eve cannot be overridden
  145.         Assert.IsTrue(typeof(Eve).IsSealed);
  146.     }
  147.    
  148.     [Test]
  149.     public void Eve_can_only_be_create_of_a_rib_of_adam()
  150.     {
  151.         Assert.Throws<ArgumentNullException>(() => Eve.GetInstance(null));
  152.     }
  153.  
  154.     [Test]
  155.     public void Eve_is_a_human()
  156.     {
  157.         Assert.IsTrue(Eve.GetInstance(Adam.GetInstance()) is Human);
  158.     }
  159.  
  160.     [Test]
  161.     public void Eve_is_a_female()
  162.     {
  163.         Assert.IsTrue(Eve.GetInstance(Adam.GetInstance()) is Female);
  164.     }
  165.  
  166.     [Test]
  167.     public void Reproduction_always_result_in_a_male_or_female()
  168.     {
  169.         Assert.IsTrue(typeof(Human).IsAbstract);
  170.     }
  171.    
  172.     [Test]
  173.     public void Human_can_reproduce_when_they_have_a_mother_and_father_and_have_a_name()
  174.     {
  175.         var adam = Adam.GetInstance();
  176.         var eve = Eve.GetInstance(adam);
  177.         var seth = new Male("Seth", eve, adam);
  178.         var azura = new Female("Azura", eve, adam);
  179.         var enos = new Male("Enos", azura, seth);
  180.  
  181.         Assert.AreEqual("Eve", eve.Name);
  182.         Assert.AreEqual("Adam", adam.Name);
  183.         Assert.AreEqual("Seth", seth.Name);
  184.         Assert.AreEqual("Azura", azura.Name);
  185.         Assert.AreEqual("Enos", ((Human)enos).Name);
  186.         Assert.AreEqual(seth, ((Human)enos).Father);
  187.         Assert.AreEqual(azura, ((Human)enos).Mother);
  188.     }
  189.    
  190.     [Test]
  191.     public void Father_and_mother_are_essential_for_reproduction()
  192.     {
  193.         // There is just 1 way to reproduce
  194.         Assert.AreEqual(1, typeof(Male).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  195.           .Where(x => x.IsPublic || x.IsAssembly).Count());
  196.         Assert.AreEqual(1, typeof(Female).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).
  197.           Where(x => x.IsPublic || x.IsAssembly).Count());
  198.        
  199.         var adam = Adam.GetInstance();
  200.         var eve = Eve.GetInstance(adam);
  201.         Assert.Throws<ArgumentNullException>(() => new Male("Seth", null, null));
  202.         Assert.Throws<ArgumentNullException>(()=> new Male("Abel", eve, null));
  203.         Assert.Throws<ArgumentNullException>(() => new Male("Seth", null, adam));
  204.         Assert.Throws<ArgumentNullException>(() => new Female("Azura", null, null));
  205.         Assert.Throws<ArgumentNullException>(() => new Female("Awan", eve, null));
  206.         Assert.Throws<ArgumentNullException>(() => new Female("Dina", null, adam));
  207.         Assert.Throws<ArgumentNullException>(() => new Female("Eve", null, null));
  208.         Assert.Throws<ArgumentNullException>(() => new Male("Adam", null, null));
  209.     }
  210.     */
  211. }*/
Add Comment
Please, Sign In to add comment