public void String8Block_Basics() { String8Block block = new String8Block(); byte[] buffer = new byte[4096]; String8 value = String8.Convert("hello", buffer); // Verify copies are persistent when the original buffer is overwritten String8 valueCopy = block.GetCopy(value); String8.Convert("there", buffer); Assert.AreEqual("there", value.ToString()); Assert.AreEqual("hello", valueCopy.ToString()); // Verify copy of String8.Empty works String8 emptyCopy = block.GetCopy(String8.Empty); Assert.IsTrue(emptyCopy.IsEmpty()); // Verify large strings are copied correctly (stored individually) value = String8.Convert(new string('A', 4096), buffer); valueCopy = block.GetCopy(value); Assert.IsTrue(value.Equals(valueCopy)); String8.Convert(new string('B', 4096), buffer); Assert.IsFalse(value.Equals(valueCopy)); // Verify storage uses multiple blocks correctly for (int i = 0; i < 1000; ++i) { value = String8.Convert(new string((char)('0' + (i % 10)), 100), buffer); valueCopy = block.GetCopy(value); Assert.IsTrue(value.Equals(valueCopy)); } // Verify conversion of strings String8 directConversion = block.GetCopy("Regular String"); Assert.AreEqual("Regular String", directConversion.ToString()); // Verify null/empty string conversion directConversion = block.GetCopy((string)null); Assert.IsTrue(directConversion.IsEmpty()); directConversion = block.GetCopy(String.Empty); Assert.IsTrue(directConversion.IsEmpty()); // Verify clear works (doesn't throw, GetCopy works afterward) block.Clear(); valueCopy = block.GetCopy("Third"); Assert.AreEqual("Third", valueCopy.ToString()); }
public void String8_Basics() { byte[] b1 = null, b2 = null, b3 = null; String8 one = String8.Copy("one", ref b1); String8 two = String8.Copy("two", ref b2); String8 one2 = String8.Copy("one", ref b3); Assert.IsFalse(one.Equals(two)); Assert.AreNotEqual(one.GetHashCode(), two.GetHashCode()); Assert.IsTrue(one.Equals(one2)); Assert.AreEqual(one.GetHashCode(), one2.GetHashCode()); Assert.IsTrue(one.CompareTo(two) < 0); Assert.IsTrue(two.CompareTo(one) > 0); Assert.AreEqual(0, one.CompareTo(one2)); Assert.AreEqual(0, one.CompareTo(one)); }
private void WriteRunSubsetStart(JsonTextWriter writer) { int depth = _bionReader.Depth; // Read and copy everything in the run until the end object except the large collections while (_bionReader.Read() && _bionReader.Depth > depth) { if (_bionReader.TokenType == BionToken.PropertyName) { String8 propertyName = _bionReader.CurrentString8(); if (propertyName.Equals(Files) || propertyName.Equals(Results)) { _bionReader.Skip(); continue; } } JsonBionConverter.WriteToken(_bionReader, writer); } // Start a results array and leave open writer.WritePropertyName("results"); writer.WriteStartArray(); }
private static TableMetadata Build(IStreamProvider streamProvider, string tableRootPath) { TableMetadata metadata = new TableMetadata(); string schemaFilePath = Path.Combine(tableRootPath, SchemaFileName); using (ITabularReader sr = TabularFactory.BuildReader(streamProvider.OpenRead(schemaFilePath), SchemaFileName)) { int nameIndex = sr.ColumnIndex("Name"); int typeIndex = sr.ColumnIndex("Type"); while (sr.NextRow()) { metadata.Schema.Add(new ColumnDetails(sr.Current(nameIndex).ToString(), TypeProviderFactory.Get(sr.Current(typeIndex).ToString()).Type)); } } using (ITabularReader mr = TabularFactory.BuildReader(streamProvider.OpenRead(Path.Combine(tableRootPath, MetadataFileName)), MetadataFileName)) { int nameIndex = mr.ColumnIndex("Name"); int contextIndex = mr.ColumnIndex("Context"); int valueIndex = mr.ColumnIndex("Value"); while (mr.NextRow()) { String8 name = mr.Current(nameIndex).ToString8(); String8 context = mr.Current(contextIndex).ToString8(); ITabularValue value = mr.Current(valueIndex); if (name.Equals("RowCount")) { metadata.RowCount = value.ToInteger(); } else { throw new NotImplementedException($"TableMetadataSerializer.Read doesn't know how to read Metadata '{name}'"); } } } metadata.Query = streamProvider.ReadAllText(Path.Combine(tableRootPath, ConfigQueryPath)); return(metadata); }
public void Comparisons() { var a = "12345678"; var b = "12345679"; var sa = String8.FromString(a); var sb = String8.FromString(b); Assert.IsFalse(sa.Equals(null)); Assert.AreEqual(System.Math.Sign(sa.CompareTo(sb)), System.Math.Sign(String.Compare(a, b, StringComparison.Ordinal))); Assert.AreEqual(System.Math.Sign(String8.Compare(sa, sb)), System.Math.Sign(String.Compare(a, b, StringComparison.Ordinal))); Assert.AreEqual(sa == sb, a == b); Assert.AreEqual(String8.Equals(sa, sb), a == b); Assert.AreEqual(sa != sb, a != b); Assert.IsTrue(sa < sb); Assert.IsTrue(sa <= sb); Assert.IsFalse(sa > sb); Assert.IsFalse(sa >= sb); }
private bool AddUniqueMembers( PackageDatabase source, int sourceMemberIndex, int targetMemberIndex, DatabaseAddResult result, ArdbVersion version) { int pathLength = result.CurrentPath.Length; SymbolDetails memberDetails = source.DeclaredMemberDetails[sourceMemberIndex]; bool hasUniqueDescendants = false; int indexToAddChildrenUnder = targetMemberIndex; // Add public types and namespaces which contain either bool addMember = false; if (memberDetails.Type == SymbolType.FrameworkTarget) { String8 encodedFrameworkNames = source.StringStore[source.DeclaredMembers.GetNameIdentifier(sourceMemberIndex)]; result.CurrentFrameworkNames = new HashSet <string>(encodedFrameworkNames.ToString().ToFrameworkNames()); } else if (memberDetails.Type == SymbolType.Namespace) { addMember = ContainsPublics(source, sourceMemberIndex); } else if (memberDetails.Type == SymbolType.Assembly) { if (result.CurrentFrameworkNames != null) { result.CurrentFrameworkNames.Clear(); } } else if (memberDetails.Type.IsType()) { addMember = memberDetails.Modifiers.HasFlag(SymbolModifier.Public); } // Add the member if it is a public type or contains them [and set 'indexInTarget' to the added member] if (addMember) { result.PublicTypeCount++; // Remap name for target StringStore [all unique names will be added anyway] int memberName = source.DeclaredMembers.GetNameIdentifier(sourceMemberIndex); String8 memberNameText = source.StringStore[memberName]; result.CurrentPath.Append((result.CurrentPath.Length > 0 ? "." : "") + memberNameText.ToString()); memberName = this.StringStore.FindOrAddString(memberNameText); string fullTypeName; HashSet <string> frameworkTargets; // See if this name is already found in the merged namespace tree if (!this.MergedMembers.TryFindChildByName(targetMemberIndex, memberName, out indexToAddChildrenUnder)) { // If not, add it, and the tree is unique because this name is indexToAddChildrenUnder = this.MergedMembers.Add(targetMemberIndex, memberName); // Identify the package which added it int packageNameIdentifier = this.StringStore.FindOrAddString(source.Identity.PackageName); this.MergedMemberSourcePackageIdentifier.Add(packageNameIdentifier); this.MergedMemberDuplicateCount.Add(1); hasUniqueDescendants = true; result.MergedTypeCount++; if (version != ArdbVersion.V1 && memberDetails.Type.IsType()) { // We have encountered a fully-qualified type name for the // very first time. We will associate this name with the // current framework target. We will also remember this // sourceMemberIndex, in order to use it as the item // to add when populating the ARDB. fullTypeName = result.CurrentPath.ToString(); result.TypeToFrameworkTargetsMap.Add(fullTypeName, new HashSet <string>()); if (result.CurrentFrameworkNames != null) { result.TypeToFrameworkTargetsMap[fullTypeName].UnionWith(result.CurrentFrameworkNames); } result.TypeNameToTypeIndexMap.Add(fullTypeName, sourceMemberIndex); } } else { // Otherwise, if this is a duplicate with another entry in the same package, still include it (different framework targets) String8 sourcePackage = this.StringStore[this.MergedMemberSourcePackageIdentifier[indexToAddChildrenUnder]]; if (version != ArdbVersion.V1 && result.CurrentFrameworkNames != null && sourcePackage.Equals(source.Identity.PackageName)) { hasUniqueDescendants = true; result.MergedTypeCount++; if (version != ArdbVersion.V1 && memberDetails.Type.IsType()) { // We have encountered a fully-qualified type name that we've seen // before. We will record the current framework target but // will already have a source member index to use to add // to the ARDB later. fullTypeName = result.CurrentPath.ToString(); frameworkTargets = result.TypeToFrameworkTargetsMap[fullTypeName]; frameworkTargets.UnionWith(result.CurrentFrameworkNames); Debug.Assert(result.TypeNameToTypeIndexMap.ContainsKey(fullTypeName)); } } else { // If this isn't unique, increment the count of copies this.MergedMemberDuplicateCount[indexToAddChildrenUnder] += 1; } } } if (!memberDetails.Type.IsType()) { int childIndex = source.DeclaredMembers.GetFirstChild(sourceMemberIndex); while (childIndex > 0) { hasUniqueDescendants |= AddUniqueMembers(source, childIndex, indexToAddChildrenUnder, result, version); childIndex = source.DeclaredMembers.GetNextSibling(childIndex); } } // Add the package which had this member to results if it wasn't unique if (hasUniqueDescendants == false) { result.SourcePackageNames[sourceMemberIndex] = this.StringStore[this.MergedMemberSourcePackageIdentifier[indexToAddChildrenUnder]]; } // Record whether this member was unique (and added) result.WasMemberAdded[sourceMemberIndex] = hasUniqueDescendants; result.CurrentPath.Length = pathLength; return(hasUniqueDescendants); }