コード例 #1
0
        public void GetDegrees_VerifyDegrees_FourLevels()
        {
            FBMember member = new FBMember();
              member.Name = "hi there";
              member.Email = "*****@*****.**";
              member.Friends = new List<FBMember>();

              member.Friends.Add(new FBMember() { Name = "hi there", Email = "*****@*****.**", Friends = new List<FBMember>() });
              member.Friends.Add(new FBMember() { Name = "hi there2", Email = "*****@*****.**", Friends = new List<FBMember>() });
              member.Friends.Add(new FBMember() { Name = "hi there3", Email = "*****@*****.**", Friends = new List<FBMember>() });

              member.Friends[0].Friends = new List<FBMember>() { new FBMember() { Name = "hi there4", Email = "*****@*****.**", Friends = new List<FBMember>() } };
              member.Friends[1].Friends = new List<FBMember>() { new FBMember() { Name = "hi there5", Email = "*****@*****.**", Friends = new List<FBMember>() } };
              member.Friends[2].Friends = new List<FBMember>() { new FBMember() { Name = "hi there6", Email = "*****@*****.**", Friends = new List<FBMember>() } };

              member.Friends[0].Friends[0].Friends = new List<FBMember>() { new FBMember() { Name = "hi there7", Email = "*****@*****.**", Friends = new List<FBMember>() } };
              member.Friends[1].Friends[0].Friends = new List<FBMember>() { new FBMember() { Name = "hi there8", Email = "*****@*****.**", Friends = new List<FBMember>() } };
              member.Friends[2].Friends[0].Friends = new List<FBMember>() { new FBMember() { Name = "hi there9", Email = "*****@*****.**", Friends = new List<FBMember>() } };
              var result = SimpleFBGraph.GetFriendDegrees(member);

              Assert.AreEqual(result[1][0].Email, "*****@*****.**");
              Assert.AreEqual(result[1][1].Email, "*****@*****.**");
              Assert.AreEqual(result[1][2].Email, "*****@*****.**");
              Assert.AreEqual(result[2][0].Email, "*****@*****.**");
              Assert.AreEqual(result[2][1].Email, "*****@*****.**");
              Assert.AreEqual(result[2][2].Email, "*****@*****.**");
              Assert.AreEqual(result[3][0].Email, "*****@*****.**");
              Assert.AreEqual(result[3][1].Email, "*****@*****.**");
              Assert.AreEqual(result[3][2].Email, "*****@*****.**");
        }
コード例 #2
0
        public void GetDegrees_OneMember_ReturnsEmptyList()
        {
            FBMember member = new FBMember();
              member.Name = "hi there";
              member.Email = "*****@*****.**";
              member.Friends = new List<FBMember>();

              var result = SimpleFBGraph.GetFriendDegrees(member);

              Assert.AreEqual(null, result);
        }
コード例 #3
0
        public static Dictionary<int, List<FBMember>> GetFriendDegrees(FBMember m)
        {
            Queue<FBMember> toVisitMembers = new Queue<FBMember>();
              Dictionary<int, List<FBMember>> degreeDict = new Dictionary<int, List<FBMember>>();
              Dictionary<string, int> visitedMembers = new Dictionary<string, int>();

              // visit all first level members
              // setting their degree and marking as visited
              foreach (FBMember member in m.Friends)
              {
            member.Degree = 1;
            toVisitMembers.Enqueue(member);
            visitedMembers.Add(member.Email, 1);
              }

              while (toVisitMembers.Count > 0)
              {
            FBMember currentFriend = toVisitMembers.Dequeue();

            if (!degreeDict.ContainsKey(currentFriend.Degree))
            {
              degreeDict[currentFriend.Degree] = new List<FBMember>() { currentFriend };
            }
            else
            {
              degreeDict[currentFriend.Degree].Add(currentFriend);
            }

            foreach (FBMember member in currentFriend.Friends)
            {
              if (!visitedMembers.ContainsKey(member.Email))
              {
            member.Degree = currentFriend.Degree + 1;
            toVisitMembers.Enqueue(member);
              }
            }
              }

              if (degreeDict.Count == 0)
              {
            return null;
              }
              else
              {
            return degreeDict;
              }
        }
コード例 #4
0
        public void GetDegrees_VerifyDegrees_TwoLevels()
        {
            FBMember member = new FBMember();
              member.Name = "hi there";
              member.Email = "*****@*****.**";
              member.Friends = new List<FBMember>();

              member.Friends.Add(new FBMember() { Name = "hi there", Email = "*****@*****.**", Friends = new List<FBMember>() });
              member.Friends.Add(new FBMember() { Name = "hi there2", Email = "*****@*****.**", Friends = new List<FBMember>() });
              member.Friends.Add(new FBMember() { Name = "hi there3", Email = "*****@*****.**", Friends = new List<FBMember>() });

              var result = SimpleFBGraph.GetFriendDegrees(member);

              Assert.AreEqual(result[1][0].Email, "*****@*****.**");
              Assert.AreEqual(result[1][1].Email, "*****@*****.**");
              Assert.AreEqual(result[1][2].Email, "*****@*****.**");
        }