Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more

with C++17 now

Code
Diff
  • template <class... Args>
    bool alwaysTrue(Args&&...) {
      return true;
    }
    
    template <class T>
    T id(T&& x)  {
      return static_cast<T&&>(x);
    }
    
  • 11 template <class... Args>
    22 bool alwaysTrue(Args&&...) {
    33 return true;
    44 }
    55
    66 template <class T>
    77 T id(T&& x) {
    8- return x;
    8+ return static_cast<T&&>(x);
    99 }
Arrays
Algorithms
Data
Code
Diff
  • function pairs(xs) {
      const d = new Set();
      for (const x of xs) {
        d.add(x);
        if (d.has(-x))
          return [x, -x];
      }
    }
    
  • 1-function pairs(arr){
    2- let sorted = arr.sort();
    3- let length = arr.length;
    4-
    5- for (let i = 0; i < length; i++) {
    6- if (sorted.includes(arr[i] * -1)) {
    7- return [sorted[i], sorted[i] * -1];
    8- }
    1+function pairs(xs) {
    2+ const d = new Set();
    3+ for (const x of xs) {
    4+ d.add(x);
    5+ if (d.has(-x))
    6+ return [x, -x];
    99 }
    1010 }
#include <stdlib.h>
#include <string.h>

char *my_strdup_1(const char *str) {
  const size_t len = strlen(str);
  char *const res = malloc(len); // <- BUG
  memcpy(res, str, len);
  return res;
}

char *my_strdup_2(const char *str) {
  const size_t len = strlen(str);
  char *const res = malloc(len); // <- BUG
  memcpy(res, str, len + 1);
  return res;
}

Without tests and a Log container...

Code
Diff
  • function profilePicture() {
      distance = (x1, y1, x2, y2) => Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
      random = t => t[~~(Math.random() * t.length)];
      var width = 56, height = 32;
      var ratio = height / width;
      var grid = [...Array(height)].map(a => []);
      for (var x = 0; x < width; x++) {
        for (var y = 0; y < height; y++) {
          var isIncluded =
            distance(width * 0.5 * ratio, height * 0.50, (x+.5) * ratio, y + .5) < height * 0.47 &&
            distance(width * 0.6 * ratio, height * 0.45, (x+.5) * ratio, y + .5) >= height * 0.41;
          grid[y].push(isIncluded ? random("£@$%€0&#¥") : ' ');
        }
      }
      return grid.map(a => a.join``).join`\n`;
    }
    
    console.log('<OUT:ESC:>' + profilePicture().replace(/\n/g, '<:LF:>'));
    
  • 1111 distance(width * 0.6 * ratio, height * 0.45, (x+.5) * ratio, y + .5) >= height * 0.41;
    1212 grid[y].push(isIncluded ? random("£@$%€0&#¥") : ' ');
    1313 }
    1414 }
    1515 return grid.map(a => a.join``).join`\n`;
    1616 }
    17+
    18+console.log('<OUT:ESC:>' + profilePicture().replace(/\n/g, '<:LF:>'));
Code
Diff
  • fn foo() -> i32 {
        2
    }
  • 11 fn foo() -> i32 {
    2- 0
    2+ 2
    33 }
fn foo() -> i32 {
    0
}

Checking fixed output.

Code
Diff
  • let foo x = 0;;
  • 1-let foo x = 0
    1+let foo x = 0;;
let foo x = 0
;
Unnamedvs. UnnamedFailed Tests

Fixed

Code
Diff
  • var foo* = -1
  • 1-var foo* = 0
    1+var foo* = -1
var foo* = 0
Code
Diff
  • public class Algorithms {
      public static int reverseInt(int n) {
        return Integer.parseInt(new StringBuilder().append(n).reverse().toString());
      }
    }
  • 11 public class Algorithms {
    22 public static int reverseInt(int n) {
    3- return Integer.parseInt(new StringBuffer(String.valueOf(n)).reverse().toString());
    4- }
    3+ return Integer.parseInt(new StringBuilder().append(n).reverse().toString());
    4+ }
    55 }
public class Adder {
    public static int add(int a, int b) {
        return a + b + 1;
    }
}
UnnamedFailed Tests

fast-check

function add1(a, b) {
  return a + b;
}

function add2(a, b) {
  return a + b + 1;
}

function add3(a, b) {
  throw new Error('boo');
}
Loading more items...