Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Generate Meta-Strings for Testing Your Application

4.83/5 (10 votes)
8 Aug 2008CPOL3 min read 1   146  
A utility for generating self-describing strings that are useful for testing an application's string handling abilities.

Introduction

A meta-string describes its own length and internal offsets. Meta-strings are useful for testing an application's ability to accept and process arbitrarily long strings without truncating them because you can tell at a glance if and where a meta-string has been truncated.

For example, suppose an application is required to handle up to 10,000 characters in a certain text box of a Properties dialog. You can easily generate a 10,000 char meta-string, paste it in, and tell at a glance if it "fits" or if it gets truncated after being saved and reloaded.

Definition of a Meta-String

A meta-string is basically a list of numbers such that each number is equal to its position in the string. The numbers in the string are separated by the character of your choice, such as a dash. Meta-strings come in two flavors; decreasing and increasing. The following increasing and decreasing meta-strings are both 60 chars long:

1-3-5-7-9-12-15-18-21-24-27-30-33-36-39-42-45-48-51-54-57-60
60-57-54-51-48-45-42-39-36-33-30-27-24-21-18-15-12-9-7-5-3-1

Properties of Increasing Meta-Strings

The numbers in an increasing meta-string tell you the length (or position) of the string at the end of each number. Therefore, an increasing string always ends with the string's total length. However, they don't always start with '1'. Sometimes they start with "-2" as in the following example. It is difficult to predict which case will occur for a given length without actually generating the string.

-2-4-6-8-11

An increasing meta-string makes it easy to tell what the new length of a string is after being truncated on the right. For example, you can tell the following string was truncated and the new length is 29. Just start with the last intact number (27) and count two more chars to the end. Unfortunately, you can't tell just by looking what the original length was. Also, if the string gets truncated at the "wrong" length (e.g. 27), you can't tell if it was truncated at all unless you know the original length.

1-3-5-7-9-12-15-18-21-24-27-3

Properties of Decreasing Meta-Strings

The numbers in a decreasing meta-string tell you the number of chars remaining in the string from the beginning of each number (i.e. including the number itself). Therefore, a decreasing string always starts with the string's total length. They always end with '1' or "2-".

If a decreasing string doesn't end with '1' or "2-", you know it has been truncated on the right. You can tell the original length by looking at the beginning of the string. You can tell how many characters were lost by looking at the end and compute the new length by subtraction.

For example, you can tell the following string originally had 255 chars. You can tell 211 chars were lost, by finding the last intact number (215) and subtracting the number of chars from that point to the end (4). Thus, the length of this string is 255 - 211 = 44.

255-251-247-243-239-235-231-227-223-219-215-

Meta-String Generator

The code that accompanies this article is a small GUI utility that generates meta-strings and puts them on the clipboard so you can paste them into other applications. The internal code has no explicit limit on the length, but the Length text box only accepts seven characters so the maximum length is 9999999 (i.e., 10,000,000 - 1). That should be enough for most applications.

It only takes a few seconds to generate a max length (9999999) string. However, it takes a very long time to paste it into Notepad for some reason. At least, Notepad will accept the entire string. Notepad++ truncates at 65,564. It also misbehaves if you try to position the cursor past 65,536. Maybe the Notepad++ developers should have done some testing with meta-strings.

History

  • 8th September, 2007: Initial post
  • 6th August, 2008: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)