Search code examples
c++unit-testinglanguage-design

What's a Good Way to Test that Identifiers aren't Being Truncated and Thereby Mixed Up?


In C++ class today, we discussed the maximum possible length of identifiers, and how the compiler will eventually stop treating variables as different, after a certain length. (My professor seems to have implied that really long identifiers are truncated.) I posted another question earlier, hoping to see if the limit is defined somewhere. My question here is a little different. Suppose I wanted to test either a practical or enforced limit on identifier name lengths. How would I go about doing so? Here's what I'm thinking of doing, but somehow it seems to be too simple.

  • Step 1: Generate at least two variables with really long names and print them to the console. If the identifier names are really that unlimited, I am not going to waste time typing them. My code should do it for me.
  • Step 2: Attempt to perform some operations with the variables, such as compare them, or any arithmetic. If the compiler stops differentiating, then in theory, certain arithmetic will break, such as x/(reallyLongA-reallyLongB), since reallyLongA and reallyLongB will be so long that the compiler will just treat them as the same thing. At that point, the division operation will become a division-by-zero, which should crash and burn horribly.

Am I approaching this correctly? Will I run out of memory before I "break" the compiler or "runtime"?


Solution

  • I don't think you need to even generate any operations on the variables.

    The following code will generate a redefinition error at compilation time;

    int name;
    int name;
    

    I'd expect you'd get the same error with

    int namewithlastsignificantcharacterhere_abc;
    int namewithlastsignificantcharacterhere_123;
    

    I'd use a macro scripting language to generate successively longer names until you got one that broke. Here's a Ruby one-liner

    C:>ruby -e "(1..2048).each{|i| puts \"int #{'variable'*i}#{i};\"}" > var.txt

    When I #include var.txt in a c file, and compile with VS2008, I get the error

    "1>c:\code\quiz\var.txt(512) : fatal error C1064: compiler limit : token overflowed internal buffer"

    and 512*8 chars is the 4096 that JRL cited.