Example #1
0
        public void TestCreateSrcMLArchiveMtM()
        {
            using (Archive srcmlArchive = new Archive()) {
                List <String> bufferList = new List <String>();
                List <String> fileList   = new List <String>();
                String        str        = "int main(){int c; c = 0; ++c;}";
                String        str2       = "int foo(){int c; c = 0; ++c;}";
                bufferList.Add(str);
                bufferList.Add(str2);

                fileList.Add("input.cpp");
                fileList.Add("input2.cpp");

                var buffandfile = bufferList.Zip(fileList, (b, f) => new { buf = b, file = f });
                foreach (var pair in buffandfile)
                {
                    using (Unit srcmlUnit = new Unit()) {
                        srcmlUnit.SetUnitBuffer(pair.buf);
                        srcmlUnit.SetUnitFilename(pair.file);

                        srcmlUnit.SetUnitLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX);
                        srcmlArchive.AddUnit(srcmlUnit);
                    }
                }

                srcmlArchive.ArchivePack();
                IntPtr        structPtr      = srcmlArchive.GetPtrToStruct();
                List <IntPtr> structArrayPtr = new List <IntPtr>();
                structArrayPtr.Add(structPtr);
                IntPtr s = new IntPtr(0);
                try {
                    s = LibSrcMLRunner.SrcmlCreateArchiveMtM(structArrayPtr.ToArray(), structArrayPtr.Count());
                }
                catch (Exception e) {
                    Console.WriteLine("EXCEPTION: {0}", e.Message);
                    Assert.True(false);
                }

                List <String> documents = new List <String>();
                for (int i = 0; i < 1; ++i)
                {
                    IntPtr docptr = Marshal.ReadIntPtr(s);
                    String docstr = Marshal.PtrToStringAnsi(docptr);
                    Marshal.FreeHGlobal(docptr);
                    documents.Add(docstr);
                    s += Marshal.SizeOf(typeof(IntPtr));
                }

                Assert.False(String.IsNullOrEmpty(documents.ElementAt(0)));
                XDocument doc   = XDocument.Parse(documents.ElementAt(0));
                var       units = from unit in doc.Descendants(XName.Get("unit", "http://www.srcML.org/srcML/src"))
                                  where unit.Attribute("filename") != null
                                  select unit;

                string file = "input.cpp";
                var    f1   = (from ele in units
                               where ele.Attribute("filename").Value == file
                               select ele);
                Assert.AreEqual("input.cpp", f1.FirstOrDefault().Attribute("filename").Value);

                string file2 = "input2.cpp";
                var    f2    = (from ele in units
                                where ele.Attribute("filename").Value == file2
                                select ele);
                Assert.AreEqual("input2.cpp", f2.FirstOrDefault().Attribute("filename").Value);
                Console.WriteLine(s);
            }
        }