public static ISet <FatCat> GenerateFatCats(int count, FatCat parent = null) { ISet <FatCat> parents = new HashSet <FatCat>(); parents.Add(parent); return(GenerateFatCats(count, parents)); }
static void Main(string[] args) { MegaCorp megaCorp = new MegaCorp(); FatCat Madea = new FatCat("Madea", 26000); FatCat Marie = new FatCat("Marie", 26000); FatCat LucasSr = new FatCat("LucasSr", 26000, Marie); FatCat Miriam = new FatCat("Miriam", 26000, Marie); FatCat Katie = new FatCat("Katie", 26000, Madea); FatCat Zack = new FatCat("Zack", 26000, Madea); WageSlave Lucas = new WageSlave("Lucas", 26000, LucasSr); WageSlave Marcus = new WageSlave("Marcus", 26000, Katie); WageSlave Markel = new WageSlave("Markel", 26000, Katie); WageSlave Ashley = new WageSlave("Ashley", 26000, Miriam); megaCorp.Add(Madea); megaCorp.Add(Marie); megaCorp.Add(LucasSr); megaCorp.Add(Miriam); megaCorp.Add(Katie); megaCorp.Add(Zack); megaCorp.Add(Lucas); megaCorp.Add(Marcus); megaCorp.Add(Markel); megaCorp.Add(Ashley); IEnumerable <string> query = from item in megaCorp.GetElements() select item.GetName(); foreach (var n in query) { Console.WriteLine(n); } }
public void GetChildrenEmpty(MegaCorp megaCorp, FatCat fatCat) { ISet <ICapitalist> children = megaCorp.GetChildren(fatCat); Assert.NotNull(children); Assert.Empty(children); }
public void FullValueEquality(string name, int salary, FatCat parent) { WageSlave firstCopy = new WageSlave(name, salary, parent); WageSlave secondCopy = new WageSlave(name, salary, parent); Assert.Equal(firstCopy, secondCopy); }
public static WageSlave GenerateWageSlave(FatCat parent = null) { string name = GenerateWageSlaveName(); int salary = GenerateSalary(); return(parent != null ? new WageSlave(name, salary, parent) : new WageSlave(name, salary)); }
public static FatCat GenerateFatCat(FatCat parent = null) { string name = GenerateFatCatName(); int salary = GenerateSalary(); return(parent != null ? new FatCat(name, salary, parent) : new FatCat(name, salary)); }
public void NoOwnerValueEquality(string name, int salary) { FatCat firstCopy = new FatCat(name, salary); FatCat secondCopy = new FatCat(name, salary); Assert.Equal(firstCopy, secondCopy); }
public static ISet <FatCat> GenerateFatCatsAtDepth(int count, int depth, FatCat parent = null) { ISet <FatCat> parents = new HashSet <FatCat>(); parents.Add(parent); return(GenerateFatCatsAtDepth(count, depth, parents)); }
public void FullValueEquality(string name, int salary, FatCat parent) { FatCat firstCopy = new FatCat(name, salary, parent); FatCat secondCopy = new FatCat(name, salary, parent); Assert.Equal(firstCopy, secondCopy); }
public void GetHierarchyInitializesChildSets(MegaCorp megaCorp, FatCat fatCat) { megaCorp.Add(fatCat); ISet <ICapitalist> children = megaCorp.GetHierarchy()[fatCat]; Assert.NotNull(children); Assert.Empty(children); }
public void GetParentsFatCat(MegaCorp megaCorp, FatCat fatCat) { megaCorp.Add(fatCat); ISet <FatCat> parents = megaCorp.GetParents(); Assert.NotEmpty(parents); Assert.Equal(1, parents.Count); }
public void GetParentsDefensiveCopy(MegaCorp megaCorp, FatCat fatCat) { ISet <FatCat> parents = megaCorp.GetParents(); parents.Add(fatCat); Assert.False(megaCorp.Has(fatCat), "#GetParents() returned a live set of parents that allowed external changes to the MegaCorp"); parents = megaCorp.GetParents(); Assert.False(parents.Contains(fatCat), "#GetParents() returned a live set of parents that allowed external changes to the MegaCorp"); }
public void FullConstructor(string name, int salary, FatCat parent) { WageSlave wageSlave = new WageSlave(name, salary, parent); Assert.Equal(name, wageSlave.GetName()); Assert.Equal(salary, wageSlave.GetSalary()); Assert.Equal(parent, wageSlave.GetParent()); Assert.True(wageSlave.HasParent(), "HasParent() did not return true when constructed with an owner"); }
public void NoOwnerConstructor(string name, int salary) { FatCat fatCat = new FatCat(name, salary); Assert.Equal(name, fatCat.GetName()); Assert.Equal(salary, fatCat.GetSalary()); Assert.Null(fatCat.GetParent()); Assert.False(fatCat.HasParent(), "HasParent() did not return false when constructed without an owner"); }
public void FullConstructor(string name, int salary, FatCat parent) { FatCat fatCat = new FatCat(name, salary, parent); Assert.Equal(name, fatCat.GetName()); Assert.Equal(salary, fatCat.GetSalary()); Assert.Equal(parent, fatCat.GetParent()); Assert.True(fatCat.HasParent(), "HasParent() did not return true when constructed with an owner"); }
public static void Print(FatCat c) { if (c == null) { Console.WriteLine("Fat cat was null?"); return; } Console.WriteLine("Fat cat has a width of " + c.FatnessWidth); }
public void GetChildrenFatCatWithParent(MegaCorp megaCorp, FatCat fatCat) { megaCorp.Add(fatCat); ISet <ICapitalist> children = megaCorp.GetChildren(fatCat); Assert.Empty(children); children = megaCorp.GetChildren(fatCat.GetParent()); Assert.True(children.Contains(fatCat), "#getChildren() returned a set that does not contain the previously-added FatCat when called with its parent"); }
public void GetParentsWageSlaveWithParent(MegaCorp megaCorp, WageSlave wageSlave) { megaCorp.Add(wageSlave); FatCat parent = wageSlave.GetParent(); ISet <FatCat> parents = megaCorp.GetParents(); Assert.NotEmpty(parents); Assert.Equal(1, parents.Count); Assert.True(parents.Contains(parent), "#getParents() returned a set that did not contain the parent of the WageSlave added to the MegaCorp"); }
/* * Methods responsible for instantiating Capitalists */ public static ICapitalist GenerateCapitalist(FatCat parent = null) { if (random.NextDouble() >= 0.5) { return(GenerateFatCat(parent)); } else { return(GenerateWageSlave(parent)); } }
public void GetChildrenDefensiveCopy(MegaCorp megaCorp, FatCat fatCat, ICapitalist capitalist) { megaCorp.Add(fatCat); ISet <ICapitalist> children = megaCorp.GetChildren(fatCat); Assert.Empty(children); children.Add(capitalist); Assert.False(megaCorp.Has(capitalist), "#GetChildren() returned a live set that allowed external changes to the MegaCorp"); children = megaCorp.GetChildren(fatCat); Assert.False(children.Contains(capitalist), "#GetChildren() returned a live set that allowed external changes to the MegaCorp"); }
static void Main(string[] args) { // Making a few cats and treating them as ICat // This is good for a demonstration, but in real life generally you won't do this ICat fatCat = new FatCat(); ICat shortCat = new ShortCat(); fatCat.Meow(); shortCat.Meow(); // Since we know we have a fat cat, we can use our fat cat printer FatCat castFatCat = fatCat as FatCat; FatCatPrinter.Print(castFatCat); // What happens if we cast our ShortCat as a FatCat? FatCat castShortCat = shortCat as FatCat; FatCatPrinter.Print(castShortCat); // What would happen if we didn't use the "as" keyword? // FatCatPrinter.Print((FatCat)shortCat); // What's the advantage of using "as" ? // We can do null checks //FatCat castShortCat2 = shortCat as FatCat; //if (castShortCat2 != null) // FatCatPrinter.Print(castShortCat2); // The "is" keyword is similar to as, but only returns true/false if (shortCat is FatCat) { FatCatPrinter.Print((FatCat)shortCat); } Console.WriteLine("---"); Console.WriteLine("--- Now using factory ---"); Console.WriteLine("---"); // More realistic example of getting cats ICat catFromFactory1 = CatFactory.GetCat(); ICat catFromFactory2 = CatFactory.GetCat(); catFromFactory1.Meow(); catFromFactory2.Meow(); // Assignment // // todo: Use both keywords "as" and "is" to safely call the FatCatPrinter.Print method // }
public void GetParentChainMatchesInternalStructure(MegaCorp megaCorp, ICapitalist capitalist) { megaCorp.Add(capitalist); FatCat parent = capitalist.GetParent(); ISet <FatCat> expected = new HashSet <FatCat>(); while (parent != null) { expected.Add(parent); parent = parent.GetParent(); } Assert.True(expected.SetEquals(megaCorp.GetParentChain(capitalist)), "#GetParentChain() returned a list that did not match the calculated structure of the arbitrary Capitalist that was just added to the MegaCorp"); }
public void GetParentsMultipleArbitraryCapitalists(MegaCorp megaCorp, ISet <ICapitalist> capitalists) { ISet <FatCat> expected = new HashSet <FatCat>(); foreach (ICapitalist capitalist in capitalists) { megaCorp.Add(capitalist); FatCat parent = capitalist.GetType() == typeof(FatCat) ? (FatCat)capitalist : capitalist.GetParent(); while (parent != null) { expected.Add(parent); parent = parent.GetParent(); } } ISet <FatCat> parents = megaCorp.GetParents(); Assert.True(expected.SetEquals(parents), "#GetParents() returned a set that did not equal the set of all parents of the added Capitalists"); }
public void AddHasParentlessFatCat(MegaCorp megaCorp, FatCat fatCat) { Assert.True(megaCorp.Add(fatCat), "#Add() returned false when called with a parent-less FatCat"); Assert.True(megaCorp.Has(fatCat), "#Has() returned false when called with a parent-less FatCat that failed to be added"); }
/* * Methods responsible for generating at a specific depth */ public static ICapitalist GenerateCapitalistAtDepth(int depth, FatCat parent = null) { return(GenerateCapitalist(GenerateFatCatAtDepth(depth, parent))); }
public void GetChildrenMultipleCapitalistsSomeWithSharedParent(MegaCorp megaCorp, FatCat parent, ISet <ICapitalist> children, ISet <ICapitalist> loose) { megaCorp.Add(parent); ISet <ICapitalist> expected = new HashSet <ICapitalist>(); foreach (ICapitalist parentless in children) { ICapitalist withParent = parentless.GetType() == typeof(FatCat) ? (ICapitalist) new FatCat(parentless.GetName(), parentless.GetSalary(), parent) : (ICapitalist) new WageSlave(parentless.GetName(), parentless.GetSalary(), parent); megaCorp.Add(withParent); expected.Add(withParent); } foreach (ICapitalist looseCapitalist in loose) { megaCorp.Add(looseCapitalist); } Assert.True(expected.SetEquals(megaCorp.GetChildren(parent)), "#getChildren() returned a set that did not equal the set of children of a previously-added FatCat after adding loose capitalists"); }
public static FatCat GenerateFatCatAtDepth(int depth, FatCat parent = null) { return(GenerateFatCat(depth > 0 ? GenerateFatCatAtDepth(depth - 1, parent) : parent)); }
public static WageSlave GenerateWageSlaveAtDepth(int depth, FatCat parent = null) { return(GenerateWageSlave(depth > 0 ? GenerateFatCatAtDepth(depth - 1, parent) : parent)); }