--- C:\Users\User\Documents\wordpress\develop.svn.wordpress.org\tests\phpunit\tests\post\wpUniquePostSlug-orig.php
+++ C:\Users\User\Documents\wordpress\develop.svn.wordpress.org\tests\phpunit\tests\post\wpUniquePostSlug.php
@@ -329,7 +329,7 @@
 			'post_name' => 'embed',
 		) );
 
-		$found = wp_unique_post_slug( 'embed', $p, 'publish', 'paage', 0 );
+		$found = wp_unique_post_slug( 'embed', $p, 'publish', 'page', 0 );
 		$this->assertSame( 'embed-2', $found );
 	}
 
@@ -347,4 +347,204 @@
 		$found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 );
 		$this->assertSame( 'embed-2', $found );
 	}
+	
+	public function post_types_provider() {
+		return array(
+					 array('page'),
+					 array('post'),
+					 array('attachment'),
+		);
+	}
+	
+	/**
+	 * @dataProvider post_types_provider
+	 * @ticket 39603
+	 */
+	public function test_sequential_slug_prefixes($post_type) {
+		$posts_num = 3;
+		$post_factory = self::factory()->post;
+		
+		for ($i = 1; $i <= $posts_num; $i++) {
+			$id = $post_factory->create( array(
+				'post_type' => $post_type,
+				'post_status' => 'publish',
+				'post_title' => "Generated {$post_type}"
+			));
+			
+			$inserted_object = $post_factory->get_object_by_id($id);
+			
+			if ($i == 1)
+				$this->assertSame("generated-{$post_type}", $inserted_object->post_name);
+			else
+				$this->assertSame("generated-{$post_type}-{$i}", $inserted_object->post_name);
+		}
+	}
+	
+	/**
+	 * @dataProvider post_types_provider
+	 * @ticket 39603
+	 */
+	public function test_exact_slug_match_should_be_used_for_prefixed_slug_generation($post_type) {	
+		$post_factory = self::factory()->post;
+		
+		$postarr_proto = array(
+			'post_type' => $post_type,
+			'post_status' => 'publish',
+			'post_title' => null,
+		);
+		
+		// create post, post-2, post-3, and post names which have 'post' substrings in their names
+		foreach (array('Post', 'Post', 'Post', 'Postman', 'Compost', 'Imposter') as $post_title) {
+			$postarr = $postarr_proto;
+			$postarr['post_title'] = $post_title;
+			$post_factory->create($postarr);
+		}
+				
+		// check if previous post names have influence on prefix generation
+		$postarr = $postarr_proto;
+		$postarr['post_title'] = 'Post';
+		$id = $post_factory->create($postarr);
+		
+		$inserted_object = $post_factory->get_object_by_id($id);
+		$this->assertSame("post-4", $inserted_object->post_name);
+	}
+	
+	/**
+	 * @dataProvider post_types_provider
+	 * @ticket 39603
+	 */
+	public function test_post_creation_time_should_not_influence_prefixed_slug_generation($post_type) {
+		$post_factory = self::factory()->post;
+		
+		$postarr = array(
+			'post_type' => $post_type,
+			'post_status' => 'publish',
+			'post_title' => null,
+		);
+		
+		$postarr['post_title'] = 'foo';
+		$post_1 = $post_factory->create($postarr);
+		
+		$postarr['post_title'] = 'bar';
+		$post_factory->create($postarr);
+		
+		// rename the slug of 'foo' to 'bar'
+		$post_factory->update_object($post_1, array('post_name' => 'bar'));
+		$renamed_post_1 = $post_factory->get_object_by_id($post_1);
+		$this->assertSame('bar-2', $renamed_post_1->post_name);
+		
+		$postarr['post_title'] = 'bar';
+		$post_3 = $post_factory->create($postarr);
+		$saved_post_3 = $post_factory->get_object_by_id($post_3);
+		$this->assertSame('bar-3', $saved_post_3->post_name);
+	}
+	/**
+	 * @ticket 39603
+	 * dataProvider post_types_provider
+	 */
+	public function test_slug_prefixes_should_fill_gaps() {
+		$post_type = 'post';
+		$post_factory = self::factory()->post;
+		$postarr_proto = array (
+			'post_type' => $post_type,
+			'post_status' => 'publish',
+			'post_title' => 'Generated Post',
+		);
+		
+		// create generated-post and generated-post-2
+		$postarr = $postarr_proto;
+		$post_factory->create($postarr);
+		$post_factory->create($postarr);
+		
+		// create a post with explicitly named slug 'generated-post-4'
+		$postarr2 = $postarr_proto;
+		$postarr2['post_name'] = 'generated-post-4';
+		$post_factory->create($postarr2);
+		
+		// next automatically found unique suffix must be between 2 and 4
+		$id = $post_factory->create($postarr);
+		$last_saved_post = $post_factory->get_object_by_id($id);
+		$this->assertSame('generated-post-3', $last_saved_post->post_name);
+	}
+	
+	public function slugs_provider() {
+		return array(
+			array(
+				'your-subject',
+				array('your-subject'),
+				'your-subject-2',
+			),
+			array(
+				'your-subject',
+				array('your-subject', 'your-subject-2'),
+				'your-subject-3',
+			),
+			array(
+				'your-subject',
+				array('your-subject', 'your-subject-3'),
+				'your-subject-2',
+			),
+			array(
+				'your-subject',
+				array('your-subject', 'your-subject-3', 'your-subject-5', 'your-subject-7', 'your-subject-9'),
+				'your-subject-2',
+			),
+		);
+	}
+	
+	/**
+	 * @ticket 39603
+	 * @dataProvider slugs_provider
+	 */
+	public function test_get_first_available_slug($base_slug, $slugs, $anticipated_slug) {
+		$this->assertSame($anticipated_slug, _get_first_available_slug($base_slug, $slugs));
+	}
+	
+	/**
+	 * As we change SAVEQUERIES constant here (super global), it must be run in a separate process
+	 * 
+	 * @ticket 39603
+	 * @dataProvider post_types_provider
+     * @runInSeparateProcess
+     */
+	public function test_constant_amount_of_sql_queries_should_be_used_for_prefixed_slug_generation($post_type) {
+		global $wpdb;
+		define('SAVEQUERIES', true);
+		
+		$inserts_num = 10;
+		
+		$postarr = array(
+			'post_type' => $post_type,
+			'post_status' => 'publish',
+			'post_title' => 'Generated Post',
+		);
+
+		$queries_first_insert = null;
+		$queries_total = 0;
+		
+		// Let's skip the first post, because it's slug can't be prefixed in a clean test environment
+		// (we only need to count queries issued for posts with prefixed slugs)
+		$id = wp_insert_post($postarr);
+		//echo "Post with id={$id} has been created" . PHP_EOL;
+		
+		for ($i = 1; $i <= $inserts_num; $i++) {
+			$old_number_of_queries = count($wpdb->queries);
+			$id = wp_insert_post($postarr);
+			
+			$queries_issued = count($wpdb->queries) - $old_number_of_queries;
+			//echo "Post #{$i} with id={$id} has been created, for which {$queries_issued} queries issued" . PHP_EOL;
+			
+			if ($queries_first_insert === null)
+				$queries_first_insert = $queries_issued;
+				
+			$queries_total += $queries_issued;
+		}
+		
+		// we are not very strict and allow some post inserts have more queries than the first post creation had
+		$queries_avg = floor($queries_total / $inserts_num);
+		
+		$this->assertLessThanOrEqual($queries_first_insert, $queries_avg,
+									 "The average number of queries should be not greater than the number of first queries on creating prefixed posts of type '{$post_type}'");
+	
+	}
 }
