Blogs

Go tests are run concurrently!

Go tests are run concurrently in their own context

I ran in a problem while writing go tests... and it looked like some parallelism or at least concurrency issues.

The initialization would run twice and not finish properly the second time. That means it would load a file from a test, a file which is rather bad (read invalid) outside of that one test.

So I looked around to see what was happening and that's how I learned that tests in go run concurrently. It sounds all good unless you want to do integration testing because such require a state which is actually not reachable if your unit tests run concurrently. But why is that?

Well... ...

Common bug in Go, maybe they did not resolve anything?

Variable shadowing, a common problem, no good solution in modern languages to avoid it.

C++ is not that bad!

With C++, and derivative, you can shadow any one variable over and over again:

int var;

void myfunc()
{
    int var;

    if(expr)
    {
        int var;

        ...
    }
}

In this example, I shadow the var variable twice within myfunc().

In itself, it doesn't hurt, but it is very often that when you do that, you did not mean to. That is, it's not unlikely that you reuse var within the function thinking it's the global variable or the first instance in myfunc() from the outermost block of the function.

I have two rules I follow in C++ ...

The new C++17 [[fallthrough]] "attribute"

Here I wanted to talk briefly about this new C++17 attribute:

[[fallthrough]];

I wanted that attribute like 30 years ago but the compiler programmers were definitely not ready to allow us to have safe coding practices...

Back then, any potential error was a beautiful feature. I'm really glad that some people pushed the idea that having tons of warnings about very possibly invalid code is a good thing long term. I am fixing some of my bugs just by compiling my code with newer versions of the compiler!

Now... I ran in a few problems with that new attribute. The fact is that it's a ...

A little bit of Go!

I changed a goroutine to make use a service which gets awaken whenever a signal happens. This is used because the signal is sent only in very complicated cases and I wanted to remove the tests from the bottom goroutine (many of the parameters are from other structures not available here.)

Here I wanted to get the basics to show how it's done.

var (
  someMutex      sync.Mutex
  someCond       *sync.Cond
  someState      bool
)

func init() {
  someCond = sync.NewCond(&someMutex)
  go someLoop()
}

func sendSignal() {
  someMutex.Lock()
  defer someMutex.Unlock()

  ...

C++: Why should I breakup my expressions?

One Line Expressions

There has been and I'm sure there still are many talks about whether to write it all on one line or break up expressions on many lines.

One Line Pros

The only pro I can think of for having everything on the same line is conciseness. For example:

*dst++ = *src++;

This is a very clear statement for most every C/C++ programmer.

I have to agree with this fact and I at times use such lines of code too...

One Line Cons

Now, I write very large libraries and programs (thousands when not ten of thousands or even hundreds of thousand lines of code....) and it ...

Meta-programming, promoting a type without changing the sign

Today I found that one of my functions would overflow. Here is a simplified version of it:

template<typename T>
T scale(T value, T position, T count)
{
    return (value * position) / count;
}

As we can see, all variables are of type T. So if the multiplication overflows, the function returns an invalid result. To fix this, I wanted to promote all the variables to the latest possible integer (keeping the sign) or double (in my case the support for floating points was just for the type float so no need for long double.)

As a result I wrote a structure using meta-programming as ...

Creating a Gantt Chart for your Project for Free

ProjectLibre Screenshot

Today I wanted to work on the schedule of a project with the proper dependencies. More or less, I had the project tasks laid out as a long list (As I would if I were the only one to work on said project) and I needed to answer two main questions:

  • How much will it cost?
  • How many resources do we need?

The other question, which is always really hard to answer as a software developer, was: Can you make it happen in 6 months? But you know the answer to that one: Of Course! (and 6 months later: Not!)

So, I remembedered seeing a manager using MS Project which is what it is. In ...

ASCII Art Pony

Isn't that cool?

I just find it really facinating that some people have an eye to draw anything with anything (in this case, a little pony with wings using just ASCII characters.)

Have you ever tried to come up with a drawing of your own that would look as good as this one?

      ,;;*;;;;,
     .-'``;-');;.
    /'  .-.  /*;;
  .'    \d    \;;               .;;;,
 / o      `    \;    ,__.     ,;*;;;*;,
 \__, _.__,'   \_.-') __)--.;;;;;*;;;;,
  `""`;;;\       /-')_) __)  `\' ';;;;;;
     ;*;;;        -') `)_)  |\ |  ;;;;*;
     ...

Getting a Proper Random Number on a Computer

Dungeons & Dragons dice have all the possible shapes that exist!

99.9% of the time, I see people using random number generator in their software.

Say you have a random function b() that generates random bits. Each time you call b(), you either get 0 or 1.

Now, in most cases people are going to get a larger number, say you write a function r() which returns 8 such bits. You could write it like this:

int r()
{
  return (b() << 7)
       | (b() << 6)
       | (b() << 5)
       | (b() << 4)
       | (b() << 3)
       | (b() << 2)
       | (b() << 1)
       | (b() << 0);
}

Assuming you have a ...

Some Math

Mean

The mean of a set of values [math](x_1, x_2, ..., x_n)[/math] is often represented by [math]\overline{x}[/math]. The calculation of the mean is:

[display]\overline{x} = \mu = {\sum_{i=1}^n x_i \over n}[/display]

The mean may make use of weights (p):

[display]\overline{x} = \mu = {\sum_{i=1}^n x_i p_i \over \sum_{i=1}^n p_i}[/display]

You may want to normalize your weights, which means that their sum will equal 1, with the following:

[display]w_i = {p_i \over {\sum_{j=1}^n p_j}}[/display]

With normalized weights, you can simplify with the following:

[display]\overline{x} = \mu = ...