Exemplo n.º 1
0
        public void DeserializeDatabase()
        {
            string       expected = @"Words:
BASEBALL, BAT, CAT, PORT, PORTS, SPORT

Links:
BASEBALL BAT WordAssociation
BAT BASEBALL WordAssociation
BAT CAT OneLetterChange
CAT BAT OneLetterChange
PORT PORTS OneLetterAddOrRemove
PORT SPORT OneLetterAddOrRemove
PORTS PORT OneLetterAddOrRemove
PORTS SPORT Anagram
SPORT PORT OneLetterAddOrRemove
SPORT PORTS Anagram
";
            StringReader reader   = new StringReader(expected);
            WordDatabase db       = WordDatabase.Deserialize(reader);

            Assert.IsTrue(db.GetAllWords().SetEquals(new HashSet <Word> {
                baseball, bat, cat, sport, ports, port
            }));
            Assert.IsTrue(db.ContainsLink("BASEBALL", "BAT"));
            Assert.IsTrue(db.ContainsLink("BAT", "BASEBALL"));
            Assert.IsTrue(db.ContainsLink("PORTS", "SPORT"));
        }
Exemplo n.º 2
0
        public void RemoveLink()
        {
            WordDatabase db = new WordDatabase();

            db.AddWords(new List <Word> {
                cat, bat, baseball, sport, ports, port
            });
            db.AddLink(batBaseball);
            db.AddLink(baseballSport);
            Assert.IsTrue(db.ContainsLink(sport, port));
            db.RemoveLink(sportPort);
            Assert.IsFalse(db.ContainsLink(sport, port));
            Assert.ThrowsException <System.Exception>(() => db.RemoveLink(portsParts));
        }
Exemplo n.º 3
0
        public void ContainsWord()
        {
            WordDatabase db = new WordDatabase();

            db.AddWords(new List <Word> {
                cat, bat, baseball
            });
            Assert.IsTrue(db.ContainsWord(cat));
            Assert.IsTrue(db.ContainsWord("BAT"));
            Assert.IsTrue(db.ContainsWord(baseball));
            Assert.IsFalse(db.ContainsWord(ports));
            Assert.IsFalse(db.ContainsWord("SPORT"));
            Assert.IsFalse(db.ContainsWord(""));
        }
Exemplo n.º 4
0
        public void ImproperWordAdd()
        {
            WordDatabase db = new WordDatabase();

            db.AddWord(cat);
            Assert.IsTrue(db.GetAllWords().SetEquals(new HashSet <Word> {
                cat
            }));
            db.AddWord(cat);
            Assert.AreEqual(1, db.GetAllWords().Count);
            Assert.IsTrue(db.GetAllWords().SetEquals(new HashSet <Word> {
                cat
            }));
        }
Exemplo n.º 5
0
        public void GetLinkBetween()
        {
            WordDatabase db = new WordDatabase();

            db.AddWords(new List <Word> {
                cat, bat, baseball, sport, ports, port
            });
            db.AddLink(batBaseball);
            db.AddLink(baseballSport);
            Assert.AreEqual(catBat, db.GetLinkBetween(cat, bat));
            Assert.AreEqual(baseballSport, db.GetLinkBetween(baseball, sport));
            Assert.AreEqual(null, db.GetLinkBetween(cat, baseball));
            Assert.AreEqual(null, db.GetLinkBetween(car, cat));
            Assert.AreEqual(null, db.GetLinkBetween("", "CAT"));
        }
Exemplo n.º 6
0
        public void ContainsLink()
        {
            WordDatabase db = new WordDatabase();

            db.AddWords(new List <Word> {
                cat, bat, baseball, sport, ports, port
            });
            db.AddLink(new Link(bat, baseball, LinkType.WordAssociation));
            Assert.IsTrue(db.ContainsLink(cat, bat));
            Assert.IsTrue(db.ContainsLink(bat, cat));
            Assert.IsTrue(db.ContainsLink(bat, baseball));
            Assert.IsTrue(db.ContainsLink("SPORT", "PORTS"));
            Assert.IsFalse(db.ContainsLink(cat, sport));
            Assert.IsFalse(db.ContainsLink("CAT", "RAT"));
        }
Exemplo n.º 7
0
        public void FindChain()
        {
            WordDatabase db = new WordDatabase();

            db.AddWords(new List <Word> {
                cat, bat, baseball, sport, ports, port
            });
            db.AddLink(new Link(bat, baseball, LinkType.WordAssociation));
            db.AddLink(new Link(sport, baseball, LinkType.WordAssociation));
            Chain chain = db.FindChain(cat, port);

            Assert.AreEqual(5, chain.Count);
            Assert.AreEqual(catBat, chain[0]);
            Assert.AreEqual(batBaseball, chain[1]);
            Assert.AreEqual(baseballSport, chain[2]);
            Assert.AreEqual(sportPorts, chain[3]);
            Assert.AreEqual(portsPort, chain[4]);
        }
Exemplo n.º 8
0
        public static WordDatabase Deserialize(TextReader reader)
        {
            WordDatabase db = new WordDatabase();

            reader.ReadLine();
            foreach (string word in reader.ReadLine().Split(','))
            {
                db.AddWord(word.Trim());
            }

            reader.ReadLine();
            reader.ReadLine();
            while (reader.Peek() >= 0)
            {
                string[] tokens = reader.ReadLine().Split(' ');
                db.AddLink(new Link(new Word(tokens[0]), new Word(tokens[1]), LinkTypes.FromString(tokens[2])));
            }
            return(db);
        }
Exemplo n.º 9
0
        public void PopulatingDatabase()
        {
            WordDatabase db = new WordDatabase();

            db.AddWord(cat);
            db.AddWords(new List <Word> {
                bat, baseball, sport, ports, port
            });
            Assert.IsTrue(db.GetAllWords().SetEquals(new HashSet <Word> {
                cat, bat, baseball, sport, ports, port
            }));
            HashSet <Link> batLinks = db.GetLinksFor(bat);

            Assert.AreEqual(1, batLinks.Count);
            Assert.IsTrue(batLinks.Contains(catBat.Reverse()));
            HashSet <Link> portsLinks = db.GetLinksFor(ports);

            Assert.AreEqual(2, portsLinks.Count);
            Assert.IsTrue(portsLinks.Contains(new Link(ports, sport, LinkType.Anagram)));
            Assert.IsTrue(portsLinks.Contains(new Link(ports, port, LinkType.OneLetterAddOrRemove)));
        }
Exemplo n.º 10
0
        public void SerializeDatabase()
        {
            string       expected = @"Words:
BASEBALL, BAT, CAT, PORT, PORTS, SPORT

Links:
BAT CAT OneLetterChange
CAT BAT OneLetterChange
PORT PORTS OneLetterAddOrRemove
PORT SPORT OneLetterAddOrRemove
PORTS PORT OneLetterAddOrRemove
PORTS SPORT Anagram
SPORT PORT OneLetterAddOrRemove
SPORT PORTS Anagram
";
            StringWriter writer   = new StringWriter();
            WordDatabase db       = new WordDatabase();

            db.AddWords(new List <Word> {
                cat, bat, baseball, sport, ports, port
            });
            db.Serialize(writer);
            Assert.AreEqual(expected, writer.GetStringBuilder().ToString());
        }