Can A Macro Do This?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gamename

    #1

    Can A Macro Do This?

    Hi

    Right now I do multiple asserts to verify multiple values:
    assert(foo==X);
    assert(foo==Y);
    assert(foo==Z);

    Is there any way to macro-ize this and do it in one call?
    MYASRT(foo, (X||Y||Z));
    ... or maybe...
    MYASRT(foo,OR,X ,Y,Z));

    TIA,
    -T
  • santosh

    #2
    Re: Can A Macro Do This?

    gamename wrote:
    Hi
    >
    Right now I do multiple asserts to verify multiple values:
    assert(foo==X);
    assert(foo==Y);
    assert(foo==Z);
    >
    Is there any way to macro-ize this and do it in one call?
    MYASRT(foo, (X||Y||Z));
    ... or maybe...
    MYASRT(foo,OR,X ,Y,Z));
    What's wrong with:

    assert(foo == X || foo == Y || foo == Z);


    Comment

    • jacob navia

      #3
      Re: Can A Macro Do This?

      santosh wrote:
      gamename wrote:
      >
      >Hi
      >>
      >Right now I do multiple asserts to verify multiple values:
      >assert(foo==X) ;
      >assert(foo==Y) ;
      >assert(foo==Z) ;
      >>
      >Is there any way to macro-ize this and do it in one call?
      >MYASRT(foo, (X||Y||Z));
      > ... or maybe...
      >MYASRT(foo,OR, X,Y,Z));
      >
      What's wrong with:
      >
      assert(foo == X || foo == Y || foo == Z);
      >
      >
      That is wrong!
      Should be:


      assert(foo == X && foo == Y && foo == Z);


      --
      jacob navia
      jacob at jacob point remcomp point fr
      logiciels/informatique
      http://www.cs.virginia.edu/~lcc-win32

      Comment

      • santosh

        #4
        Re: Can A Macro Do This?

        jacob navia wrote:
        santosh wrote:
        >gamename wrote:
        >>
        >>Hi
        >>>
        >>Right now I do multiple asserts to verify multiple values:
        >>assert(foo==X );
        >>assert(foo==Y );
        >>assert(foo==Z );
        >>>
        >>Is there any way to macro-ize this and do it in one call?
        >>MYASRT(foo, (X||Y||Z));
        >> ... or maybe...
        >>MYASRT(foo,OR ,X,Y,Z));
        >>
        >What's wrong with:
        >>
        > assert(foo == X || foo == Y || foo == Z);
        >>
        >>
        >
        That is wrong!
        Should be:
        >
        >
        assert(foo == X && foo == Y && foo == Z);
        Oops yes. You are right.

        In my defence I got sidetracked by the "MYASRT" macro presented by the
        OP, where he uses the OR operator, instead of the AND.

        Comment

        • jacob navia

          #5
          Re: Can A Macro Do This?

          santosh wrote:
          jacob navia wrote:
          >
          >santosh wrote:
          >>gamename wrote:
          >>>
          >>>Hi
          >>>>
          >>>Right now I do multiple asserts to verify multiple values:
          >>>assert(foo== X);
          >>>assert(foo== Y);
          >>>assert(foo== Z);
          >>>>
          >>>Is there any way to macro-ize this and do it in one call?
          >>>MYASRT(foo , (X||Y||Z));
          >>> ... or maybe...
          >>>MYASRT(foo,O R,X,Y,Z));
          >>What's wrong with:
          >>>
          >> assert(foo == X || foo == Y || foo == Z);
          >>>
          >>>
          >That is wrong!
          >Should be:
          >>
          >>
          > assert(foo == X && foo == Y && foo == Z);
          >
          Oops yes. You are right.
          >
          In my defence I got sidetracked by the "MYASRT" macro presented by the
          OP, where he uses the OR operator, instead of the AND.
          >
          Yes but now that I think about it...

          How can foo be 3 different things at the same time as in the
          original code???
          >>>assert(foo== X);
          >>>assert(foo== Y);
          >>>assert(foo== Z);
          That can't be right!


          --
          jacob navia
          jacob at jacob point remcomp point fr
          logiciels/informatique
          http://www.cs.virginia.edu/~lcc-win32

          Comment

          • santosh

            #6
            Re: Can A Macro Do This?

            jacob navia wrote:
            santosh wrote:
            >jacob navia wrote:
            >>
            >>santosh wrote:
            >>>gamename wrote:
            >>>>
            >>>>Hi
            >>>>>
            >>>>Right now I do multiple asserts to verify multiple values:
            >>>>assert(foo= =X);
            >>>>assert(foo= =Y);
            >>>>assert(foo= =Z);
            >>>>>
            >>>>Is there any way to macro-ize this and do it in one call?
            >>>>MYASRT(fo o, (X||Y||Z));
            >>>> ... or maybe...
            >>>>MYASRT(foo, OR,X,Y,Z));
            >>>What's wrong with:
            >>>>
            >>> assert(foo == X || foo == Y || foo == Z);
            >>>>
            >>>>
            >>That is wrong!
            >>Should be:
            >>>
            >>>
            >> assert(foo == X && foo == Y && foo == Z);
            >>
            >Oops yes. You are right.
            >>
            >In my defence I got sidetracked by the "MYASRT" macro presented by
            >the OP, where he uses the OR operator, instead of the AND.
            >>
            >
            Yes but now that I think about it...
            >
            How can foo be 3 different things at the same time as in the
            original code???
            >>>assert(foo== X);
            >>>assert(foo== Y);
            >>>assert(foo== Z);
            >
            That can't be right!
            Yes. The sequence of assert invocations as presented seem redundant. Of
            course some code could occur between the calls or the OP might have
            just presented this as an example to enquire about writing complex
            expressions with assert.

            Comment

            • gamename

              #7
              Re: Can A Macro Do This?

              Yes. The sequence of assert invocations as presented seem redundant. Of
              course some code could occur between the calls or the OP might have
              just presented this as an example to enquire about writing complex
              expressions with assert.
              Correct. The example is simplified for that purpose.

              Comment

              • =?ISO-8859-1?Q?Bj=F6rn_Paetzel?=

                #8
                Re: Can A Macro Do This?

                santosh schrieb:
                >How can foo be 3 different things at the same time as in the
                >original code???
                > >>>assert(foo== X);
                > >>>assert(foo== Y);
                > >>>assert(foo== Z);
                >>
                >That can't be right!
                >
                Yes. The sequence of assert invocations as presented seem redundant. Of
                course some code could occur between the calls or the OP might have
                just presented this as an example to enquire about writing complex
                expressions with assert.
                Maybe he wanted to something like this:

                assert(foo==X== Y==Z);

                /* ;) */

                --
                OMG,-10==10 in linux!

                Comment

                • CBFalconer

                  #9
                  Re: Can A Macro Do This?

                  jacob navia wrote:
                  santosh wrote:
                  >jacob navia wrote:
                  >>
                  .... snip ...
                  >>>
                  >> assert(foo == X && foo == Y && foo == Z);
                  >>
                  .... snip ...
                  >
                  Yes but now that I think about it...
                  >
                  How can foo be 3 different things at the same time as in the
                  original code???
                  >
                  >>>>assert(foo= =X);
                  >>>>assert(foo= =Y);
                  >>>>assert(foo= =Z);
                  >
                  That can't be right!
                  #define foo n++
                  #enum {X, Y, Z);
                  int n = 0;

                  Now it passes the assert :-)

                  --
                  Chuck F (cbfalconer at maineline dot net)
                  <http://cbfalconer.home .att.net>
                  Try the download section.


                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • Francine.Neary@googlemail.com

                    #10
                    Re: Can A Macro Do This?

                    On Nov 28, 3:17 pm, jacob navia <ja...@nospam.c omwrote:
                    santosh wrote:
                    jacob navia wrote:
                    >
                    santosh wrote:
                    >gamename wrote:
                    >
                    >>Hi
                    >
                    >>Right now I do multiple asserts to verify multiple values:
                    >>assert(foo==X );
                    >>assert(foo==Y );
                    >>assert(foo==Z );
                    >
                    >>Is there any way to macro-ize this and do it in one call?
                    >>MYASRT(foo, (X||Y||Z));
                    >> ... or maybe...
                    >>MYASRT(foo,OR ,X,Y,Z));
                    >What's wrong with:
                    >
                    > assert(foo == X || foo == Y || foo == Z);
                    >
                    That is wrong!
                    Should be:
                    >
                    assert(foo == X && foo == Y && foo == Z);
                    >
                    Oops yes. You are right.
                    >
                    In my defence I got sidetracked by the "MYASRT" macro presented by the
                    OP, where he uses the OR operator, instead of the AND.
                    >
                    Yes but now that I think about it...
                    >
                    How can foo be 3 different things at the same time as in the
                    original code???
                    >>>assert(foo== X);
                    >>>assert(foo== Y);
                    >>>assert(foo== Z);
                    >
                    That can't be right!
                    Perhaps foo is a macro with side-effects?
                    >
                    --
                    jacob navia
                    jacob at jacob point remcomp point fr
                    logiciels/informatiquehtt p://www.cs.virginia .edu/~lcc-win32

                    Comment

                    • jacob navia

                      #11
                      Re: Can A Macro Do This?

                      CBFalconer wrote:
                      jacob navia wrote:
                      >santosh wrote:
                      >>jacob navia wrote:
                      >>>
                      ... snip ...
                      >>> assert(foo == X && foo == Y && foo == Z);
                      ... snip ...
                      >Yes but now that I think about it...
                      >>
                      >How can foo be 3 different things at the same time as in the
                      >original code???
                      >>
                      >>>>>assert(foo ==X);
                      >>>>>assert(foo ==Y);
                      >>>>>assert(foo ==Z);
                      >That can't be right!
                      >
                      #define foo n++
                      #enum {X, Y, Z);
                      int n = 0;
                      >
                      Now it passes the assert :-)
                      >
                      A macro with side effects within an
                      assert() expression.

                      That is *really* a bad programming style. Yes
                      everything is possible but *that* would be an abomination,
                      and a useless one, since the assert macro would NOT
                      increment the "counter" if NDEBUG was defined!


                      --
                      jacob navia
                      jacob at jacob point remcomp point fr
                      logiciels/informatique
                      http://www.cs.virginia.edu/~lcc-win32

                      Comment

                      • Peter Nilsson

                        #12
                        Re: Can A Macro Do This?

                        gamename <namesagame-use...@yahoo.co mwrote:
                        Hi
                        >
                        Right now I do multiple asserts to verify multiple values:
                        assert(foo==X);
                        assert(foo==Y);
                        assert(foo==Z);
                        >
                        Is there any way to macro-ize this and do it in one call?
                        MYASRT(foo, (X||Y||Z));
                        ... or maybe...
                        MYASRT(foo,OR,X ,Y,Z));
                        assert(foo == X || foo == Y || foo == Z);

                        --
                        Peter

                        Comment

                        • Ben Pfaff

                          #13
                          Re: Can A Macro Do This?

                          Someone wrote:
                          assert(foo==X);
                          assert(foo==Y);
                          assert(foo==Z);
                          CBFalconer <cbfalconer@yah oo.comwrites:
                          #define foo n++
                          #enum {X, Y, Z);
                          int n = 0;
                          >
                          Now it passes the assert :-)
                          Is assert guaranteed to evaluate its argument only once (without
                          NDEBUG)?

                          (Also, enum is not a preprocessor directive, and its list of
                          enumeration values ends in }).
                          --
                          char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
                          ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
                          =b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
                          2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

                          Comment

                          • Barry Schwarz

                            #14
                            Re: Can A Macro Do This?

                            On Wed, 28 Nov 2007 16:45:40 +0100, Björn Paetzel <news@kolrabi.d e>
                            wrote:
                            >santosh schrieb:
                            >
                            >>How can foo be 3 different things at the same time as in the
                            >>original code???
                            >> >>>assert(foo== X);
                            >> >>>assert(foo== Y);
                            >> >>>assert(foo== Z);
                            >>>
                            >>That can't be right!
                            >>
                            >Yes. The sequence of assert invocations as presented seem redundant. Of
                            >course some code could occur between the calls or the OP might have
                            >just presented this as an example to enquire about writing complex
                            >expressions with assert.
                            >
                            >Maybe he wanted to something like this:
                            >
                            >assert(foo==X= =Y==Z);
                            >
                            >/* ;) */
                            Since == is not transitive in the same sense = is, it seems unlikely.


                            Remove del for email

                            Comment

                            • Philip Potter

                              #15
                              Re: Can A Macro Do This?

                              Barry Schwarz wrote:
                              On Wed, 28 Nov 2007 16:45:40 +0100, Björn Paetzel <news@kolrabi.d e>
                              wrote:
                              >
                              >santosh schrieb:
                              >>
                              >>>How can foo be 3 different things at the same time as in the
                              >>>original code???
                              >>> >>>assert(foo== X);
                              >>> >>>assert(foo== Y);
                              >>> >>>assert(foo== Z);
                              >>>>
                              >>>That can't be right!
                              >>Yes. The sequence of assert invocations as presented seem redundant. Of
                              >>course some code could occur between the calls or the OP might have
                              >>just presented this as an example to enquire about writing complex
                              >>expressions with assert.
                              >Maybe he wanted to something like this:
                              >>
                              >assert(foo==X= =Y==Z);
                              >>
                              >/* ;) */
                              >
                              Since == is not transitive in the same sense = is, it seems unlikely.
                              A relation R is transitive if a R b && b R c implies a R c for all
                              a,b,c. I believe this applies to == and it's even appropriate for =
                              because it isn't a relation.

                              Comment

                              Working...